OSSP CVS Repository

ossp - Difference in ossp-pkg/lmtp2nntp/lmtp2nntp.c versions 1.11 and 1.12
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/lmtp2nntp/lmtp2nntp.c 1.11 -> 1.12

--- lmtp2nntp.c  2001/08/07 14:58:02     1.11
+++ lmtp2nntp.c  2001/08/08 09:26:28     1.12
@@ -747,12 +747,16 @@
 lmtp_rc_t prepareheader(void *_ctx)
 {
     lmtp2nntp_t *ctx = (lmtp2nntp_t *)_ctx;
-    char *cpName;
-    char *cpValue;
-    char *cpRem;
-    char *cp;
-    char *azNewsgroups;
-    int   asNewsgroups;
+    char  *cpName;
+    char  *cpValue;
+    char  *cpRem;
+    char  *cp;
+    char  *azNewsgroups;
+    int    asNewsgroups;
+    char **aHeaders;
+    int    i;
+    char  *cpCut;
+    char  *cpWrap;
 
                                 /****************
                                  * PARSE HEADER *                                   FIXME make it nice
@@ -782,7 +786,7 @@
     while (str_parse(ctx->message.cpHeaders, "m/^([\\w-]+?:)\\s*([\\S \\t]*?)\\n(.*)/s", &cpName, &cpValue, &cpRem)) {
         free(ctx->message.cpHeaders);
         ctx->message.cpHeaders = cpRem;
-        fprintf(stderr, "DEBUG: raw    Name(%s) = Value(%s)\n", cpName, cpValue);
+        //fprintf(stderr, "DEBUG: raw    Name(%s) = Value(%s)\n", cpName, cpValue);
         argz_add(&ctx->message.azHeaders, &ctx->message.asHeaders, cpName);
         argz_add(&ctx->message.azHeaders, &ctx->message.asHeaders, cpValue);
     }
@@ -794,6 +798,7 @@
                                  * MANIPULATE HEADER *                              FIXME make it nice
                                  *********************/
 
+    /* throw out headers we don't want anymore */
     cp = NULL;
     while ((cp = argz_next(ctx->message.azHeaders, ctx->message.asHeaders, cp)) != NULL) {
         if (strcasecmp("To:", cp) == 0) {
@@ -806,6 +811,7 @@
         } //FIXME what bad things can happen here (odd number of argz, delete fails ...
     }
 
+    /* create a proper Newsgroups: header */
     cp = NULL;
     azNewsgroups = NULL;
     asNewsgroups = 0;
@@ -817,14 +823,70 @@
     argz_add(&ctx->message.azHeaders, &ctx->message.asHeaders, "Newsgroups:");
     argz_add(&ctx->message.azHeaders, &ctx->message.asHeaders, azNewsgroups);
 
+    /* merge name/value pairs into single string */
+    argz_add(&ctx->message.azHeaders, &ctx->message.asHeaders, ""); /* append empty string */
+    if ((aHeaders = (char **)malloc((argz_count(ctx->message.azHeaders,
+        ctx->message.asHeaders) + 1) * sizeof(char *))) == NULL)
+        exit(1); //FIXME
+    argz_extract(ctx->message.azHeaders, ctx->message.asHeaders, aHeaders);
+    i=0;
+    while(1) {
+        if ((cp = aHeaders[++i]) == NULL)
+            break;
+        *(cp-1) = ' ';
+        if ((cp = aHeaders[++i]) == NULL)
+            break;
+        // *(cp-1) = '\n';
+    }
+
+    /* fold headers */
+//FIXME where to place this defines best
+#define WRAPAT 120
+#define WRAPUSING "\n    "
     cp = NULL;
     while ((cp = argz_next(ctx->message.azHeaders, ctx->message.asHeaders, cp)) != NULL) {
-        cpName = cp;
-        cp = argz_next(ctx->message.azHeaders, ctx->message.asHeaders, cp);
-        cpValue = cp;
-        fprintf(stderr, "DEBUG: cooked Name(%s) = Value(%s)\n", cpName, cpValue);
+        if (strlen(cp) >= WRAPAT) {
+            cpRem = cp;
+            cpWrap = NULL;
+            while (strlen(cpRem) >= WRAPAT) {
+                for (i = WRAPAT; i >= 1 && (cpRem[i] != ' ') && (cpRem[i] != '\t'); i--);
+                if (i == 0)
+                    i = WRAPAT; /* sorry, hard cut at non-whitespace */
+                if (i < WRAPAT)
+                    i++; /* we don't care about the whitespace itself */
+                cpCut = str_dup(cpRem, i);
+                if (cpWrap == NULL) {
+                    if ((cpWrap = (char *)malloc(strlen(cpCut)+strlen(WRAPUSING)+1)) == NULL)
+                        exit(1); //FIXME
+                    *cpWrap = '\0';
+                }
+                else {
+                    if ((cpWrap = (char *)realloc(cpWrap, strlen(cpWrap)+strlen(cpCut)+strlen(WRAPUSING)+1)) == NULL)
+                        exit(1); //FIXME
+                    strcat(cpWrap, WRAPUSING);
+                }
+                strcat(cpWrap, cpCut);
+                free(cpCut);
+                cpRem += i;
+            }
+            for (i = 0; i < strlen(cpRem) && ((cpRem[i] == ' ') || (cpRem[i] == '\t')); i++);
+            cpRem += i;
+            if (strlen(cpRem) > 0) {
+                if ((cpWrap = (char *)realloc(cpWrap, strlen(cpWrap)+strlen(cpRem)+strlen(WRAPUSING)+1)) == NULL)
+                    exit(1); //FIXME
+                strcat(cpWrap, WRAPUSING);
+                strcat(cpWrap, cpRem);
+            }
+            argz_delete(&ctx->message.azHeaders, &ctx->message.asHeaders, cp);
+            argz_insert(&ctx->message.azHeaders, &ctx->message.asHeaders, cp, cpWrap);
+            free(cpWrap);
+//fprintf(stderr, "DEBUG: after  wrap = ***%s***\n", cp);
+        }
     }
 
+    argz_stringify(ctx->message.azHeaders, ctx->message.asHeaders, '\n');
+    fprintf(stderr, "DEBUG: flat headers = ***%s***\n", ctx->message.azHeaders);
+
     return LMTP_OK;
 }
 

CVSTrac 2.0.1