]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/network/serv_network.c
Use IOBuffer with its StrBuf + const char* inside instead of having two of them
[citadel.git] / citadel / modules / network / serv_network.c
index 7f5d5dd306e6a172880f9d0c0c63c6201116a69f..300b1b6bc37aaaffac17d18a94caebcc446f2951 100644 (file)
@@ -682,6 +682,9 @@ void network_spool_msg(long msgnum, void *userdata) {
                 */
                msg = CtdlFetchMessage(msgnum, 1);
                if (msg != NULL) {
+                       int len, rlen;
+                       char *pCh;
+
                        if (msg->cm_fields['V'] == NULL){
                                /* local message, no enVelope */
                                StrBuf *Buf;
@@ -711,9 +714,26 @@ void network_spool_msg(long msgnum, void *userdata) {
                        if (msg->cm_fields['U'] == NULL) {
                                msg->cm_fields['U'] = strdup("(no subject)");
                        }
-                       snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
-                       free(msg->cm_fields['U']);
-                       msg->cm_fields['U'] = strdup(buf);
+                       
+                       len  = strlen(msg->cm_fields['U']);
+                       rlen = strlen(CC->room.QRname);
+                       pCh  = strstr(msg->cm_fields['U'], CC->room.QRname);
+                       if ((pCh == NULL) ||
+                           (*(pCh + rlen) != ']') ||
+                           (pCh == msg->cm_fields['U']) ||
+                           (*(pCh - 1) != '[')
+                               )
+                       {
+                               char *pBuff;
+
+                               rlen += len + 4;
+                               pBuff = malloc (rlen * sizeof(char));
+
+                               snprintf(pBuff, rlen, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
+                               free(msg->cm_fields['U']);
+                               msg->cm_fields['U'] = pBuff;
+                       }
+                       /* else we won't modify the buffer, since the roomname is already here. */
 
                        /* Set the recipient of the list message to the
                         * email address of the room itself.
@@ -1924,7 +1944,6 @@ void receive_spool(int *sock, char *remote_nodename) {
        int download_len = 0L;
        int bytes_received = 0L;
        char buf[SIZ];
-       static char pbuf[IGNET_PACKET_SIZE];
        char tempfilename[PATH_MAX];
        char permfilename[PATH_MAX];
        int plen;
@@ -1998,13 +2017,17 @@ void receive_spool(int *sock, char *remote_nodename) {
                
                if (buf[0] == '6') {
                        plen = extract_int(&buf[4], 0);
-                       if (sock_read(sock, pbuf, plen, 1) < 0) {
+                       StrBuf *pbuf = NewStrBuf();
+                       if (socket_read_blob(sock, pbuf, plen, CLIENT_TIMEOUT) != plen) {
+                               CtdlLogPrintf(CTDL_INFO, "Short read from peer; aborting.\n");
                                fclose(fp);
                                unlink(tempfilename);
+                               FreeStrBuf(&pbuf);
                                return;
                        }
-                       fwrite((char *) pbuf, plen, 1, fp);
+                       fwrite(ChrPtr(pbuf), plen, 1, fp);
                        bytes_received += plen;
+                       FreeStrBuf(&pbuf);
                }
        }
 
@@ -2031,7 +2054,6 @@ void receive_spool(int *sock, char *remote_nodename) {
        }
 
        CtdlLogPrintf(CTDL_DEBUG, "%s\n", buf);
-       CtdlLogPrintf(CTDL_NOTICE, "Received %ld octets from <%s>\n", download_len, remote_nodename);
 
        /*
         * Now move the temp file to its permanent location.
@@ -2158,9 +2180,9 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
        }
        
        CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
-       CCC->sReadBuf = NewStrBuf();
+       CCC->SBuf.Buf = NewStrBuf();
        CCC->sMigrateBuf = NewStrBuf();
-       CCC->sPos = NULL;
+       CCC->SBuf.ReadWritePointer = NULL;
 
        /* Read the server greeting */
        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
@@ -2197,7 +2219,7 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
 
        sock_puts(&sock, "QUIT");
 bail:  
-       FreeStrBuf(&CCC->sReadBuf);
+       FreeStrBuf(&CCC->SBuf.Buf);
        FreeStrBuf(&CCC->sMigrateBuf);
        if (sock != -1)
                sock_close(sock);