diff src/fetch.c @ 180:09ca6eb5c7ff noffle

[svn] * TODO,src/client.c,src/client.h,src/fetch.c,src/fetch.h,src/noffle.c: Improve error checking during fetches. A fetch is now aborted immediately if the connection times out or if an unexpected response arrives. This should fix problems with articles appearing in the wrong group, and possibly other mysterious happenings.
author bears
date Wed, 09 May 2001 12:33:43 +0100
parents 22b81617d427
children 9854ea5f295f
line wrap: on
line diff
--- a/src/fetch.c	Wed May 09 12:15:31 2001 +0100
+++ b/src/fetch.c	Wed May 09 12:33:43 2001 +0100
@@ -1,7 +1,7 @@
 /*
   fetch.c
 
-  $Id: fetch.c 236 2000-12-05 19:50:09Z enz $
+  $Id: fetch.c 279 2001-05-09 11:33:43Z bears $
 */
 
 #if HAVE_CONFIG_H
@@ -59,7 +59,7 @@
     return TRUE;
 }
 
-void
+Bool
 Fetch_getNewGrps( void )
 {
     time_t t;
@@ -71,14 +71,14 @@
     if ( ! Utl_getStamp( &t, file ) )
     {
         Log_err( "Cannot read %s. Please run noffle --query groups", file );
-        return;
+        return FALSE;
     }
     Log_inf( "Updating groupinfo" );
-    Client_getNewgrps( &t );
+    return Client_getNewgrps( &t );
 }
 
 /* Databases open on entry, closed on exit. */
-static void
+static Bool
 fetchNewArts( const char *name, FetchMode mode )
 {
     int next, first, last, refetch;
@@ -88,7 +88,7 @@
         Log_err( "Could not change to group %s", name );
 	if ( Lock_gotLock() )
 	    Lock_closeDatabases();
-        return;
+        return FALSE;
     }
     Client_rmtFirstLast( &first, &last );
     Cont_read( name );
@@ -101,7 +101,7 @@
         Cont_write();
         Grp_setFirstLast( name, Cont_first(), Cont_last() );
 	Lock_closeDatabases();
-        return;
+        return TRUE;
     }
     if ( first == 0 && last == 0 )
     {
@@ -109,7 +109,7 @@
         Cont_write();
         Grp_setFirstLast( name, Cont_first(), Cont_last() );
 	Lock_closeDatabases();
-        return;
+        return TRUE;
     }
     if ( next > last + 1 )
     {
@@ -136,21 +136,21 @@
     Log_inf( "Getting remote overviews %lu-%lu for group %s",
              first, last, name );
     Lock_closeDatabases();
-    Client_getOver( name, first, last, mode );
+    return Client_getOver( name, first, last, mode );
 }
 
-void
+Bool
 Fetch_getNewArts( const char *name, FetchMode mode )
 {
     if ( ! Lock_openDatabases() )
     {
         Log_err( "Could not open message base" );
-        return;
+        return FALSE;
     }
-    fetchNewArts( name, mode );
+    return fetchNewArts( name, mode );
 }
 
-void
+Bool
 Fetch_updateGrps( void )
 {
     FetchMode mode;
@@ -161,7 +161,7 @@
     if ( ! Lock_openDatabases() )
     {
         Log_err( "Could not open message base" );
-        return;
+        return FALSE;
     }
     Fetchlist_read();
     size = Fetchlist_size();
@@ -170,31 +170,35 @@
         Fetchlist_element( &name, &mode, i );
         if ( strcmp( Grp_server( name ), fetch.serv ) == 0 )
 	{
-            fetchNewArts( name, mode );
+            if ( ! fetchNewArts( name, mode ) )
+		return FALSE;
 	    if ( ! Lock_openDatabases() )
 	    {
 		Log_err( "Could not open message base" );
-		return;
+		return FALSE;
 	    }
 	}
     }
     Lock_closeDatabases();
+    return TRUE;
 }
 
-static void
+static Bool
 fetchMessageList( const char *list, int *artcnt, int artmax )
 {
     const char *p;
     Str msgId;
 
     ASSERT( Lock_gotLock() );
-    Client_retrieveArtList( list, artcnt, artmax );
+    if ( ! Client_retrieveArtList( list, artcnt, artmax ) )
+	return FALSE;
     p = list;
     while ( ( p = Utl_getLn( msgId, p ) ) )
         Req_remove( fetch.serv, msgId );
+    return TRUE;
 }
 
-void
+Bool
 Fetch_getReq_( void )
 {
     Str msgId;
@@ -202,6 +206,7 @@
     DynStr *fetchList;
     const char *p;
     int count = 0, artcnt = 0, artmax = 0;
+    Bool res;
 
     ASSERT( fetch.ready );
     Log_dbg( "Retrieving articles marked for download" );
@@ -212,7 +217,7 @@
 	if ( list != NULL )
 	    del_DynStr( list );
         Log_err( "Out of memory in Fetch_get_Req_");
-	return;
+	return FALSE;
     }
 
     /*
@@ -223,7 +228,7 @@
     if ( ! Lock_openDatabases() )
     {
         Log_err( "Could not open message base" );
-        return;
+        return FALSE;
     }
 
     if ( Req_first( fetch.serv, msgId ) )
@@ -239,20 +244,22 @@
 
     /* Retrieve in groups of up to size MAX_ARTICLE_CMDS_QUEUED. */
     p = DynStr_str( list );
-    while ( ( p = Utl_getLn( msgId, p ) ) != NULL )
+    res = TRUE;
+    while ( res && ( p = Utl_getLn( msgId, p ) ) != NULL )
     {
 	DynStr_appLn( fetchList, msgId );
 	if ( ++count % MAX_ARTICLE_CMDS_QUEUED == 0 )
 	{
-	    fetchMessageList( DynStr_str( fetchList ), &artcnt, artmax );
+	    res = fetchMessageList( DynStr_str( fetchList ), &artcnt, artmax );
 	    DynStr_clear( fetchList );
 	}
     }
-    fetchMessageList( DynStr_str( fetchList ), &artcnt, artmax );
+    res = res && fetchMessageList( DynStr_str( fetchList ), &artcnt, artmax );
 
     del_DynStr( fetchList );
     del_DynStr( list );
     Lock_closeDatabases();
+    return res;
 }
 
 static void
@@ -296,13 +303,15 @@
     }
 }
 
-void
+Bool
 Fetch_postArts( void )
 {
     DynStr *s;
     Str msgId, errStr, sender;
     const char *txt;
+    Bool res;
 
+    res = TRUE;
     s = new_DynStr( 10000 );
     if ( Out_first( fetch.serv, msgId, s ) )
     {
@@ -310,24 +319,35 @@
         do
         {
             txt = DynStr_str( s );
-            Out_remove( fetch.serv, msgId );
-            if ( ! Client_postArt( msgId, txt, errStr ) )
-            {
-                Utl_cpyStr( sender, Cfg_mailTo() );
-                if ( strcmp( sender, "" ) == 0
-                     && ! Prt_searchHeader( txt, "SENDER", sender )
-                     && ! Prt_searchHeader( txt, "X-NOFFLE-X-SENDER",
-                                            sender ) /* see server.c */
-                     && ! Prt_searchHeader( txt, "FROM", sender ) )
-                    Log_err( "Article %s has no From/Sender/X-Sender field",
-                             msgId );
-                else
-                    returnArticleToSender( sender, errStr, txt );
-            }
+	    if ( ! Client_postArt( msgId, txt, errStr ) )
+	    {
+		res = FALSE;
+		break;
+	    }
+	    
+	    /*
+	     * OK, no server communication SNAFU during post. Now, do we
+	     * get an error response? If so, try to return article to sender.
+	     */
+	    Out_remove( fetch.serv, msgId );
+	    if ( errStr[0] != '\0' )
+	    {
+		Utl_cpyStr( sender, Cfg_mailTo() );
+		if ( strcmp( sender, "" ) == 0
+		     && ! Prt_searchHeader( txt, "SENDER", sender )
+		     && ! Prt_searchHeader( txt, "X-NOFFLE-X-SENDER",
+					    sender ) /* see server.c */
+		     && ! Prt_searchHeader( txt, "FROM", sender ) )
+		    Log_err( "Article %s has no From/Sender/X-Sender field",
+			     msgId );
+		else
+		    returnArticleToSender( sender, errStr, txt );
+	    }
         }
         while ( Out_next( msgId, s ) );
     }
     del_DynStr( s );
+    return res;
 }
 
 Bool