[PATCH 1/1] slrnpull: use "Lines" value from header to read article body.

ms <[email protected]>
Newsgroups gmane.network.slrn.user
Message-ID <[email protected]>
To minimize "Connection %s lost" because of broken nntp server send a
line contains only dot character when it should send double dot, this
patch use a "Lines" value from article header to get all article body.

Another know bug is when article body contain signatures and in
signatures there is a line contain only dot character.

i should warn you that this patch only tested by myself and only to one
nntp server.

------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
01-use-lines.patch (text/x-patch, 5.9 KB)
diff --git a/src/nntplib.c b/src/nntplib.c
index aa8881c..62be96e 100644
--- a/src/nntplib.c
+++ b/src/nntplib.c
@@ -1140,6 +1140,41 @@ int nntp_body_cmd (NNTP_Type *s, NNTP_Artnum_Type n, char *msgid)
    return _nntp_num_or_msgid_cmd (s, "BODY", n, msgid);
 }
 
+static int read_and_malloc(NNTP_Type *s, char *line, unsigned int line_sz,
+                           unsigned int *buffer_len,
+                           unsigned int *buffer_len_max, char **mbuf)
+{
+   int status;
+   unsigned int len;
+
+   status = nntp_read_line (s, line, line_sz);
+   if (status <= 0)
+      return status;
+
+   len = strlen (line);
+
+   if (len + *buffer_len + 4 > *buffer_len_max)
+     {
+        char *new_mbuf;
+
+        *buffer_len_max += 4096 + len;
+        new_mbuf = slrn_realloc ((*mbuf), *buffer_len_max, 0);
+
+        if (new_mbuf == NULL)
+          {
+             return -1;
+          }
+        (*mbuf) = new_mbuf;
+     }
+
+   strcpy ((*mbuf) + *buffer_len, line); /* safe */
+   *buffer_len = *buffer_len + len;
+   (*mbuf) [(*buffer_len)++] = '\n';
+   (*mbuf) [*buffer_len] = 0;
+
+   return 1;
+}
+
 char *nntp_read_and_malloc (NNTP_Type *s)
 {
    char line [NNTP_BUFFER_SIZE];
@@ -1150,8 +1185,57 @@ char *nntp_read_and_malloc (NNTP_Type *s)
    mbuf = NULL;
    buffer_len_max = buffer_len = 0;
 
-   while (1 == (status = nntp_read_line (s, line, sizeof(line))))
+   status = read_and_malloc(s, line, NNTP_BUFFER_SIZE, &buffer_len,
+                            &buffer_len_max, &mbuf);
+   while (1 == status)
      {
+        status = read_and_malloc(s, line, NNTP_BUFFER_SIZE, &buffer_len,
+                                 &buffer_len_max, &mbuf);
+     }
+
+   if (status == 0)
+     {
+	if (mbuf == NULL)
+	  mbuf = slrn_strmalloc ("", 0);
+	
+	return mbuf;
+     }
+
+   slrn_free (mbuf);
+   nntp_discard_output (s);
+
+   return NULL;
+}
+
+char *nntp_n_read_and_malloc (NNTP_Type *s, int n_lines)
+{
+   char line [NNTP_BUFFER_SIZE];
+   char *mbuf;
+   unsigned int buffer_len, buffer_len_max;
+   int status;
+
+   mbuf = NULL;
+   buffer_len_max = buffer_len = 0;
+
+   while (n_lines >= 0)
+     {
+	status = nntp_read_line (s, line, sizeof(line));
+        if (status < 0)
+          {
+             slrn_free (mbuf);
+             nntp_discard_output (s);
+             return NULL;
+          }
+        if (0 == status)
+          {
+             if (line[0] == '.' && n_lines == 0)
+                break;
+
+             line[0] = '.';
+             line[1] = '\n';
+             line[2] = '0';
+          }
+
 	unsigned int len;
 
 	len = strlen (line);
@@ -1176,6 +1260,14 @@ char *nntp_read_and_malloc (NNTP_Type *s)
 	buffer_len += len;
 	mbuf [buffer_len++] = '\n';
 	mbuf [buffer_len] = 0;
+        --n_lines;
+     }
+
+   /* read signatures */
+   while (1 == status)
+     {
+	status = read_and_malloc (s, line, NNTP_BUFFER_SIZE, &buffer_len,
+                                  &buffer_len_max, &mbuf);
      }
 
    if (status == 0)
diff --git a/src/nntplib.h b/src/nntplib.h
index d6b9475..da674bb 100644
--- a/src/nntplib.h
+++ b/src/nntplib.h
@@ -101,6 +101,7 @@ extern int nntp_body_cmd (NNTP_Type *s, NNTP_Artnum_Type, char *);
 extern int nntp_article_cmd (NNTP_Type *s, NNTP_Artnum_Type, char *);
 
 extern char *nntp_read_and_malloc (NNTP_Type *);
+extern char *nntp_n_read_and_malloc (NNTP_Type *, int n_lines);
 
 extern void (*NNTP_Connection_Lost_Hook) (NNTP_Type *);
 extern int (*NNTP_Authorization_Hook) (char *, int, char **, char **);
diff --git a/src/slrnpull.c b/src/slrnpull.c
index f44cb16..ec58608 100644
--- a/src/slrnpull.c
+++ b/src/slrnpull.c
@@ -1238,7 +1238,7 @@ static int write_head_and_body (Active_Group_Type *g, NNTP_Artnum_Type n, /*{{{*
 
 /*}}}*/
 
-static int fetch_body (NNTP_Type *s, char **body) /*{{{*/
+static int fetch_body (NNTP_Type *s, Slrn_XOver_Type *xov, char **body) /*{{{*/
 {
    int status;
    
@@ -1252,17 +1252,27 @@ static int fetch_body (NNTP_Type *s, char **body) /*{{{*/
    
    if (status != OK_BODY)
      return 0;
-   
-   if (NULL == (*body = nntp_read_and_malloc (s)))
-     return -1;
+
+   if (xov->lines <= 0)
+     {
+        if (NULL == (*body = nntp_read_and_malloc (s)))
+           return -1;
+     }
+   else
+     {
+        (*body) = nntp_n_read_and_malloc(s, xov->lines);
+        if (!(*body))
+           return -1;
+     }
    
    return 0;
 }
 
 /*}}}*/
 
-static int get_bodies (NNTP_Type *s, NNTP_Artnum_Type *numbers, /*{{{*/
-		       char **heads, char **bodies, unsigned int num)
+static int _get_bodies (NNTP_Type *s, NNTP_Artnum_Type *numbers, /*{{{*/
+		        char **heads, char **bodies, Slrn_XOver_Type *xovs,
+                        unsigned int num)
 {
    unsigned int i;
    char buf[256], *b;
@@ -1296,13 +1306,35 @@ static int get_bodies (NNTP_Type *s, NNTP_Artnum_Type *numbers, /*{{{*/
 	if (heads [i] == NULL)
 	  continue;
 
-	if (-1 == fetch_body (s, bodies + i))
-	  return -1;
+        if (xovs)
+          {
+             if (-1 == fetch_body (s, xovs + i, bodies + i))
+                return -1;
+          }
+        else
+          {
+             if (-1 == fetch_body (s, NULL, bodies + i))
+                return -1;
+          }
      }
    
    return 0;
 }
+/*}}}*/
+
+static int get_bodies (NNTP_Type *s, NNTP_Artnum_Type *numbers, /*{{{*/
+		       char **heads, char **bodies, unsigned int num)
+{
+   _get_bodies(s, numbers, heads, bodies, NULL, num);
+}
+/*}}}*/
 
+static int get_bodies_with_xov (NNTP_Type *s, NNTP_Artnum_Type *numbers, /*{{{*/
+		       char **heads, char **bodies, Slrn_XOver_Type *xovs,
+                       unsigned int num)
+{
+   _get_bodies(s, numbers, heads, bodies, xovs, num);
+}
 /*}}}*/
 
 static void free_header_data (Slrn_Header_Type *h)
@@ -1591,7 +1623,7 @@ static int get_articles (NNTP_Type *s, Active_Group_Type *g, NNTP_Artnum_Type *n
    ret = 0;
    
    if ((g->headers_only) ||
-       (-1 != get_bodies (s, numbers, heads, bodies, num)))
+       (-1 != get_bodies_with_xov (s, numbers, heads, bodies, xovs, num)))
      {
 	fp = open_xover_file (g, "a");
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.