]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/network/serv_network.c
CtdlThreadSchedule() considered harmful. Eliminate. Replaced all
[citadel.git] / citadel / modules / network / serv_network.c
index 8b0df4ec175b0e24ba055233409e437a1a290ff0..c7092954208ddd0ec1de6b89ef480c80cb664589 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$ 
- *
  * This module handles shared rooms, inter-Citadel mail, and outbound
  * mailing list processing.
  *
@@ -647,7 +645,19 @@ void network_spool_msg(long msgnum, void *userdata) {
                 */
                msg = CtdlFetchMessage(msgnum, 1);
                if (msg != NULL) {
-
+                       if (msg->cm_fields['V'] == NULL){
+                               /* local message, no enVelope */
+                               StrBuf *Buf;
+                               Buf = NewStrBuf();
+                               StrBufAppendBufPlain(Buf, msg->cm_fields['O'], -1, 0);
+                               StrBufAppendBufPlain(Buf, HKEY("@"), 0);
+                               StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
+                               
+                               msg->cm_fields['K'] = SmashStrBuf(&Buf);
+                       }
+                       else {
+                               msg->cm_fields['K'] = strdup (msg->cm_fields['V']);
+                       }
                        /* Set the 'List-ID' header */
                        if (msg->cm_fields['L'] != NULL) {
                                free(msg->cm_fields['L']);
@@ -1873,53 +1883,57 @@ void receive_spool(int *sock, char *remote_nodename) {
        }
        download_len = extract_long(&buf[4], 0);
 
-       bytes_received = 0L;
-       fp = fopen(tempfilename, "w");
-       if (fp == NULL) {
-               CtdlLogPrintf(CTDL_CRIT, "cannot open download file locally: %s\n",
-                       strerror(errno));
-               return;
-       }
-
-       while (bytes_received < download_len) {
-               /*
-                * If shutting down we can exit here and unlink the temp file.
-                * this shouldn't loose us any messages.
-                */
-               if (CtdlThreadCheckStop())
-               {
-                       fclose(fp);
-                       unlink(tempfilename);
-                       return;
-               }
-               snprintf(buf, sizeof buf, "READ %ld|%ld",
-                       bytes_received,
-                    ((download_len - bytes_received > IGNET_PACKET_SIZE)
-                ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
-               if (sock_puts(sock, buf) < 0) {
-                       fclose(fp);
-                       unlink(tempfilename);
+       if (download_len>0) {
+               bytes_received = 0L;
+               fp = fopen(tempfilename, "w");
+               if (fp == NULL) {
+                       CtdlLogPrintf(CTDL_CRIT, "cannot open download file locally: %s\n",
+                                     strerror(errno));
                        return;
                }
-               if (sock_getln(sock, buf, sizeof buf) < 0) {
-                       fclose(fp);
-                       unlink(tempfilename);
-                       return;
-               }
-               if (buf[0] == '6') {
-                       plen = extract_long(&buf[4], 0);
-                       if (sock_read(sock, pbuf, plen, 1) < 0) {
+
+               CtdlLogPrintf(CTDL_DEBUG, "For this download we are expecting %d bytes\n", download_len);
+               while (bytes_received < download_len) {
+                       /*
+                        * If shutting down we can exit here and unlink the temp file.
+                        * this shouldn't loose us any messages.
+                        */
+                       if (CtdlThreadCheckStop())
+                       {
                                fclose(fp);
                                unlink(tempfilename);
                                return;
                        }
-                       fwrite((char *) pbuf, plen, 1, fp);
-                       bytes_received = bytes_received + plen;
+                       snprintf(buf, sizeof buf, "READ %ld|%ld",
+                                bytes_received,
+                                ((download_len - bytes_received > IGNET_PACKET_SIZE)
+                                 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
+                       
+                       if (sock_puts(sock, buf) < 0) {
+                               fclose(fp);
+                               unlink(tempfilename);
+                               return;
+                       }
+                       if (sock_getln(sock, buf, sizeof buf) < 0) {
+                               fclose(fp);
+                               unlink(tempfilename);
+                               return;
+                       }
+                       
+                       if (buf[0] == '6') {
+                               plen = extract_long(&buf[4], 0);
+                               if (sock_read(sock, pbuf, plen, 1) < 0) {
+                                       fclose(fp);
+                                       unlink(tempfilename);
+                                       return;
+                               }
+                               fwrite((char *) pbuf, plen, 1, fp);
+                               bytes_received = bytes_received + plen;
+                       }
                }
-       }
-
-       fclose(fp);
 
+               fclose(fp);
+       }
        /* Last chance for shutdown exit */
        if (CtdlThreadCheckStop())
        {
@@ -2059,7 +2073,7 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
        CtdlLogPrintf(CTDL_DEBUG, "network: polling <%s>\n", node);
        CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
 
-       sock = sock_connect(host, port, "tcp");
+       sock = sock_connect(host, port);
        if (sock < 0) {
                CtdlLogPrintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
                network_talking_to(node, NTT_REMOVE);
@@ -2200,15 +2214,10 @@ void create_spool_dirs(void) {
  * 
  * Run through the rooms doing various types of network stuff.
  */
-void *network_do_queue(void *args) {
+void network_do_queue(void) {
        static time_t last_run = 0L;
        struct RoomProcList *ptr;
        int full_processing = 1;
-       struct CitContext networkerCC;
-
-       /* Give the networker its own private CitContext */
-       CtdlFillSystemContext(&networkerCC, "network");
-       citthread_setspecific(MyConKey, (void *)&networkerCC );
 
        /*
         * Run the full set of processing tasks no more frequently
@@ -2228,7 +2237,7 @@ void *network_do_queue(void *args) {
         * with a static variable instead.
         */
        if (doing_queue) {
-               return NULL;
+               return;
        }
        doing_queue = 1;
 
@@ -2303,19 +2312,6 @@ void *network_do_queue(void *args) {
        }
 
        doing_queue = 0;
-
-       /* Reschedule this task to happen again periodically, unless the thread system indicates
-        * that the server is shutting down.
-        */
-       if (!CtdlThreadCheckStop()) {
-               CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK,
-                       network_do_queue, NULL, time(NULL) + 60
-               );
-       }
-       else {
-               CtdlLogPrintf(CTDL_DEBUG, "network: Task STOPPED.\n");
-       }
-       return NULL;
 }
 
 
@@ -2384,6 +2380,24 @@ int network_room_handler (struct ctdlroom *room)
        return 0;
 }
 
+void *ignet_thread(void *arg) {
+       struct CitContext ignet_thread_CC;
+
+       CtdlLogPrintf(CTDL_DEBUG, "ignet_thread() initializing\n");
+       CtdlFillSystemContext(&ignet_thread_CC, "IGnet Queue");
+       citthread_setspecific(MyConKey, (void *)&ignet_thread_CC);
+
+       while (!CtdlThreadCheckStop()) {
+               network_do_queue();
+               CtdlThreadSleep(60);
+       }
+
+       CtdlClearSystemContext();
+       return(NULL);
+}
+
+
+
 
 /*
  * Module entry point
@@ -2397,11 +2411,9 @@ CTDL_MODULE_INIT(network)
                CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
                CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
                CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
-               CtdlRegisterRoomHook(network_room_handler);
+               CtdlRegisterRoomHook(network_room_handler);
                CtdlRegisterCleanupHook(destroy_network_queue_room);
+               CtdlThreadCreate("SMTP Send", CTDLTHREAD_BIGSTACK, ignet_thread, NULL);
        }
-       else
-               CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK, network_do_queue, NULL, 0);
-       /* return our Subversion id for the Log */
-       return "$Id$";
+       return "network";
 }