šŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.nntp
1 messages
1 total messages Started by JeffRelf.F-M.FM Fri, 20 Aug 2010 04:50
--
#83188
Author: JeffRelf.F-M.FM
Date: Fri, 20 Aug 2010 04:50
276 lines
15510 bytes
<PRE>
The following ( from ā€œ JeffRelf.F-M.FM/X.CPP ā€ ) is ♪ for my eyes only ♪
Sill, should someone care ( ha ha ), I've crossĀ·posted it.

Ā« The following MicroĀ·Soft Visual C++ 9 code downloads thousands of posts ( full body and header ),
  99 at a time.  The ā€œGo()ā€ function is explained afterwards. Ā»
  
  if ( anumTop && nReq ) {
    //  ā€œanumTopā€ is the topĀ·most ArticleĀ·Number in the newsĀ·group
    //  and ā€œnReqā€ is the number of posts to download.
    Base = nReq ; __int64 artNum = anumTop ;  int  tĀ·cntĀ·Dead = 0, tĀ·nGot = 0 ;

    gBatch:
    __LnP  P, B = P = __LnP( BĀ·ShĀ· );
    Loop( er( 99, Base ) )
      //  Request upĀ·to 99 posts ( body and header, ā€œpipelinedā€ )
      //  Glorb.COM ( INN, 10 dollars per YEAR ) yields 9 thousand posts in  38 Seconds !
      //  BlockNews.NET ( Diablo, 3 dollars per LIFETIME ) takes about  8 Minutes  to do the same.
      P += _Str( P, "Article %I64d\r\n", artNum -- );
      
    szSend = P - B ;
    Base -= LLL, cntĀ·Dead = nGot = 0, Decode = 1, Arts = 1, cntĀ·ArtsToGo = LLL, Go(0);
    *BĀ·Sh = 0, ShTaskBar ;
      
    if ( ErrDia ) {
      //  The download failed, report it.
      int nDwn = nReq - ( Base + cntĀ·ArtsToGo );
      Sh( L"\nErrDia: %c%d%c  at ā€œ %s  %I64d ā€;  got %s articles in %s."
      , bgĀ·Ch, ErrDia, textĀ·Ch, Tb_Hit->Newsgroup, 1 + anumTop - nDwn, ā€ž( nDwn ), Ago( ( Secs - Mark ) / _24_60_60 ) );
      if ( rv = cntĀ·Dead + tĀ·cntĀ·Dead )
        //  Report the number of articles the server didn't have ( usless data ).
        Sh(L"ā€œ%sā€ didn't have %d articles.", Serv->Nic, rv );
      Quit  }
    tĀ·nGot += nGot, tĀ·cntĀ·Dead += cntĀ·Dead ;
    
    if ( Base )
      //  Get ( upĀ·to ) another 99 posts.
      goto gBatch;
      
    nGot = tĀ·nGot, cntĀ·Dead = tĀ·cntĀ·Dead ;
    
    if ( !nGot && ( initĀ·Group || nReq >= maxĀ·bookĀ·marks ) ) {
      //  Apparently, all articles were authorĀ·canceled ( this never happens ).
      Sh( L"\n\n%cNo articles were available", bgĀ·Ch );  Quit  } } }

Ā« The following is the ā€œGo()ā€ function ( to down|upĀ·load posts|eMails ).
  When data is expected, it calls ā€œServed()ā€.
  On success, ā€œServed()ā€ and ā€œGo()ā€ return true. Ā»

  //  ā€œBĀ·In[]ā€ holds upĀ·to .5 megaĀ·bytes of data from the server, per read.
  //  Other buffers are UTFĀ·16 versions of this buffer.
  const int MaxHeader = 512 * 1024 ;
  uchar  ChĀ·EĀ·In, BĀ·In[ MaxHeader ];  _LnP  BĀ·Head, PĀ·In, EĀ·In ;
  int szSend, nReq, Base, szII, EĀ­3Ā·Was, EĀ­3Ā·Is, Append, EĀ­2 = 13, EĀ­1 = 10 ;
  
  #include < Winsock2.H>
  #pragma comment( lib, "Ws2_32.LIB")
  
  #define Patience( Seconds ) ( Due = Secs + ( Seconds ), Started = _Secs )
  #define Split( E ) { EĀ·InĀ· = E ; goto reĀ·Check ;  }
  #define Rtn( Err )  return EĀ­3Ā·Is = 0, Append = 0, !( ErrDia = Err )
  
int Served() { static rv ; _LnP  p, p1, p2, EĀ·InĀ· ;

  if ( !Append )
    //  In order to apply the specified charset ( e.g. UTFĀ·8 ) to the entire header,
    //  ā€œAppendā€ is true when parsing a header so ā€œBĀ·Headā€ will point to the start of the header,
    //  instead of advancing ( along with ā€œPĀ·Inā€ ) through the .5 megaĀ·byte buffer ( ā€œBĀ·Inā€ ) of 
    //  data received from the server ( containing some 20Ā·odd posts|IMAPĀ·eMails, last one incomplete ).
    BĀ·Head = PĀ·In ;
    
  int reĀ·Fill = PĀ·In + 5 > EĀ·In, reĀ·Split = EĀ­3Ā·Is != EĀ­3Ā·Was, Sz = 0, Room ;
  
  if ( !reĀ·Fill && !reĀ·Split )
    //  Do nothing, return true ( success, no error, ā€œ !( ErrDia = 0 ) ā€ ) because
    //  there's no need to get more data from the server.
    //  Ā·AndĀ· no need to guarantee* the integrity of terminator.
    //  [ *: by detecting a possiblyĀ·incomplete terminator and (re)splitting it off ]
    
    //  When ā€œEĀ­3Ā·Isā€ is true, the terminator is "\r\n.\r\n" ( NNTP ) or "\r\n.__" ( IMAP )
    //  Otherwise, it's "\r\n".  ā€œEĀ­3Ā·Wasā€ indicates the type of previous splitĀ·out.
    Rtn(0);
    
  //  (Re)SpliceĀ·In the splitĀ·off ( incomplete ) line|terminator.
  EĀ­3Ā·Was = EĀ­3Ā·Is, *EĀ·In = ChĀ·EĀ·In, EĀ·In += szII, szII = ChĀ·EĀ·In = *EĀ·In = 0 ;
  
  if ( reĀ·Fill ) {
    //  Data is expected, read from the IMAP|NNTP|SMTP server.
    Recv:
    
    if ( rv = BĀ·Head - BĀ·In )
      //  Make room for the read, discard already used data.
      memmove( BĀ·In, BĀ·Head, EĀ·In - BĀ·Head ), BĀ·Head -= rv, PĀ·In -= rv, EĀ·In -= rv ;
      
    if ( Connected < 0 ) {
      //  This  Never  happens.
      Sh(L"No connection.");  Rtn(-1);  }

    Patience(8);  //  Wait 8 seconds for the read event.
    while ( Secs < Due && !( gEvent( WM_USER + 2 ) && *P_C2 == FD_READ ) );

    if ( Cancel ) {  //  User aborted the read.
      Cancel = 0, Sh( L"\n―― %c Manual Cancel %c ――", bgĀ·Ch, textĀ·Ch ); Rtn(-1000);  }
      
    *EĀ·In = 0, Room = MaxHeader - ( EĀ·In - BĀ·In ) - 1 ;
    
    Sz = recv( Connected, __LnP( EĀ·In ), Room, 0 );
    
    if ( Sz <= 0 ) {
      //  ā€œrecv()ā€ failed.
      if ( Secs < Due && WSAGetLastError() == WSAEWOULDBLOCK ) {
      
        //  When downloading megaĀ·bytes  Fast, ā€œSleep(750)ā€ is needed.
        //  Otherwise, the WiFi adapter  Dies ! Bad Bad Bad, very Bad ! Bad.
        //  Oddly, this ā€œSleep(750)ā€ doesn't slowdown the download.
        Sleep(750);  goto Recv;  }
        
      Sh( L"\nTimed Out, %s.", Ago( ( Secs - Started ) / _24_60_60 ) );  Rtn(-2);  }
      
    EĀ·In += Sz, *EĀ·In = ChĀ·EĀ·In = 0 ;  }
    
  if ( p2 = EĀ·In - 1, p2 - 1 < PĀ·In )
     //  Less than 2 bytes in the buffer ( this never happens ).
     Split( PĀ·In )
  
  //  Find the start of the ( incomplete ) line at the end of the buffer.
  while ( --p2 > PĀ·In && EĀ·In - p2 < 2000 && !( *p2 == 13 && p2[1] == 10 ) );

  if ( *p2 != 13 || p2[1] != 10 )
    //  Only one line and it's not even complete ( this never happens ).
    Split( p2 == PĀ·In ? PĀ·In : EĀ·In - ( EĀ·In[-1] == 13 ) )
    
  if ( p = p2 + 1, !EĀ­3Ā·Is || *++p && ( *p != '.' || *++p && ( *p != EĀ­2 || *++p && *p != EĀ­1 ) ) )
    //  Thanks to the parens and ā€œShortĀ·Circuit Evaluationā€, the line above is a trickĀ·ass mircoĀ·loop.
    //  A set of parens  May or May Not  push the stack, incrementing ā€œpā€ each time.
    
    //  ā€œ Split( p2 + 2 ) ā€, below, prevents an incomplete line at the end of the buffer ( ā€œBĀ·Inā€ ).
    //  Splits off at the start of the ( incomplete ) line.
    
    //  At this point, either the terminator is just "\r\n" ( i.e. ā€œEĀ­3Ā·Isā€ is false )
    //  or there might be just one byte at the start of the line and, though incomplete,
    //  it's already known that it won't mactch the 3 byte terminator ( ".\r\n" | ".__" ).
    Split( p2 + 2 )

  if ( p1 = p2 - 1, p1 - 1 < PĀ·In )
    //  Buffer lacks a complete line ( never happens ). 
    Split( PĀ·In )
    
  //  Find ā€œthe last complete lineā€ in the buffer ( i.e. ā€œ p1 + 2 ā€ ).
  while ( --p1 > PĀ·In && p2 - p1 < 2000 && !( *p1 == 13 && p1[1] == 10 ) );
  
  if ( *p1 != 13 || p1[1] != 10 )
    //  Buffer lacks a complete line ( never happens ). 
    Split( p1 == PĀ·In ? PĀ·In : p2 )
    
  //  Guarantee an intact 5 byte terminator ( "\r\n.\r\n" | "\r\n.__" ).
  //  If it's not intact, split it off from the buffer.  Otherewise, include it.
  Split( 2 + ( p1[2] == '.' && p1[3] == EĀ­2 && p1[4] == EĀ­1 ? p2 : p1 ) )
  
  reĀ·Check:
  
  if ( PĀ·In + 5 > EĀ·InĀ· )
    //  ā€œreĀ·Fillā€, there isn't enough data to work with, read more data ( instead of doing a split off ).
    goto Recv;
    
  //  SplitĀ·Off the incomplete line|terminator and return true ( success ).
  szII = EĀ·In - EĀ·InĀ·, EĀ·In = EĀ·InĀ·, ChĀ·EĀ·In = *EĀ·In, *EĀ·In = 0 ;  Rtn(0);  }
 
int GotĀ·__() { static rv ; int Sz, Q = 0, Base64 = 0, Type = 0 ; const int BinBody = 0 ; FILE *fp = 0 ;
  _LnP  P, EĀ·Hdr = 0, EĀ·Art = 0, BĀ·NxtĀ·Ln, EĀ·Enc ;
  
  if ( EĀ­3Ā·Is = 1, !Served() )
    //  Fail, didn't get the expected data.
    return ErrDia -= 40, 0 ;
    
  NextLn:
  
  BĀ·NxtĀ·Ln = 0, P = PĀ·In - 1 ;
  
  while( ++P < EĀ·In )
    if ( P + 3 <= EĀ·In && *P == 13 && P[1] == 10 ) {
      if ( P + 4 <= EĀ·In && P[2] == 13 && P[3] == 10 ) {
        if ( EĀ·Hdr = P + 4, P + 7 <= EĀ·In && P[4] == '.' && P[5] == EĀ­2 && P[6] == EĀ­1 ) EĀ·Art = P + 7 ;  break ;  }
      if ( P + 5 <= EĀ·In && P[2] == '.' && P[3] == EĀ­2 && P[4] == EĀ­1 ) { EĀ·Hdr = P + 2, EĀ·Art = P + 5 ;  break ;  }
      BĀ·NxtĀ·Ln = P + 2 ;  break ;  }
  if ( _EqiN( __LnP( PĀ·In ), "Content-Type:" , 13 ) )  Type = 1 ;
  else if ( Type && !( *PĀ·In == 9 || *PĀ·In == 32 ) )  Type = 0 ;
  if ( Type && PĀ·In + 10 <= EĀ·In )  { *strĀ·CharSet = 0 ;  Set_IdenMime( PĀ·In, __LnP( "CharSet=" ), strĀ·CharSet );
    if ( *strĀ·CharSet ) {
      if ( Arts || anArt || eMailĀ·Down )
        *infĀ·CharSet = L'•',    
        rv = er( 14, szStr( strĀ·CharSet ) ), wmemmove( infĀ·CharSet+1, strĀ·CharSet, rv ), infĀ·CharSet[ ++rv ] = 0 ;
      FndCS( recĀ·CharSet, 0 );  } }
  if ( _EqiN( __LnP( PĀ·In ), "Content-Transfer-Encoding:", 26 ) )
    Q = ChĀ»( "Quoted-Printable", PĀ·In ) > 32, Base64 = ChĀ»( "Base64", PĀ·In ) > 32;
  if ( EĀ·Hdr ) { ShĀ·In( BĀ·Head, EĀ·Hdr, 0 ), PĀ·In = EĀ·Hdr ;  if ( EĀ·Art ) goto EĀ·ArtĀ· ;  goto Body ;  }
  if ( BĀ·NxtĀ·Ln ) { PĀ·In = BĀ·NxtĀ·Ln ; goto NextLn ;  }
  if ( PĀ·In = EĀ·In, Append = EĀ­3Ā·Is = 1, !Served() )  return ErrDia -= 60, 0 ;  goto NextLn ; 
  Body: nxtChunk:  EĀ·Enc = EĀ·In, P = PĀ·In - 1 ;
  while( ++P < EĀ·In )
    if ( P + 5 <= EĀ·In && *P == 13 && P[1] == 10 && P[2] == '.' && P[3] == EĀ­2 && P[4] == EĀ­1 ) {
     EĀ·Enc = P, EĀ·Art = P + 5 ;  break ;  }
  if ( Sz = EĀ·Enc - PĀ·In )
    if ( BinBody ) {  if ( !fp )  fp = fopen( L"Bin.DAT", L"wb" );  fwrite( PĀ·In, 1, Sz, fp );  }
    else {
      if ( Q || Base64 ) {  _LnP  P = PĀ·In, E = EĀ·Enc ;  char Ch_Saved = *E ;  *E = 0 ;
        if ( Q )  Sz -= _unQ( P );
        if ( Base64 ) {  _LnP  D = P ;   uint X = 0, By = 0, Cnt = 0 ;
          While( Ch ) {  if ( Ch <= 32 || Ch > 'z' )  continue ;
            X <<= 6 ;  X |= A_63 [ Ch ];  if ( Ch != '=' )  By ++ ;  
            if ( ++ Cnt < 4 )  continue;  _LnP  XP = ( _LnP ) & X ;  XP += 3 ;
            Loop( By * 6 / 8 )  rv = * -- XP, * D ++ = rv ;  X = By = Cnt = 0 ; }
          Sz -= E - D ;  }  * E = Ch_Saved ;  }
      ShĀ·In( PĀ·In, PĀ·In + Sz, 1 );  }
  if ( EĀ·Art ) { if ( fp ) fclose( fp );  EĀ·ArtĀ·:  PĀ·In = EĀ·Art ;  return 1 ;  }
  if ( PĀ·In = EĀ·In, EĀ­3Ā·Is = 1, !Served() ) { if ( fp ) fclose( fp ); return ErrDia -= 70, 0 ;  }
  goto nxtChunk ;  }
        
  double Mark ;  __int64 anumBot ;  
  #define  ShTaskBar  SetWindowText( Win, BĀ·Sh )
  #define Ret( E ) { ErrDia -= E ;  goto ResetRtn ;  }
int Go( LnP S, ... ) { if ( ErrDia = 0, Connected < 0 || Protocol == SNTP ) Ret( 100 ) 
  int  rv = 0, Asking ;  Expecting = MsgĀ·Up ? 2 : !aHdr && !anArt && !Arts && !eMailĀ·Down && !aPage ;
  if ( Protocol == IMAP ) EĀ­2 = EĀ­1 = '_' ; else EĀ­2 = 13, EĀ­1 = 10 ;
  Asking = Arts ? 2 : !!S, ShĀ·ReBuff( S );
  if ( !Asking )  goto Listen ;  _LnP  BĀ·Send = _LnP( BĀ·ShĀ· );
  if ( Asking == 1 ) {
    szSend = WideCharToMultiByte( CP_UTF8, 0, BĀ·Sh, -1, __LnP( BĀ·Send ), mxBytesSh, 0 , 0 ) - 1 ;
    memmove( BĀ·Send + szSend, "\r\n", 3 ), szSend += 2 ;  }
  int cntĀ·Sent = send( Connected, __LnP( BĀ·Send ), szSend, 0 );
  int Err = cntĀ·Sent == szSend ? 0 : WSAGetLastError();  
  if ( !Err && !iBoReport ) { rv = 1, ChĀ»( i0, BĀ·Sh );
    Loop(11)  if ( Ch ) rv++, ChĀ»( i0, P + CR¦LF + CR&LF ); else break ;
    if ( !MsgĀ·Up && Expecting )  Expecting = rv ;
    rv = PĀ» - BĀ·Sh, wmemmove( BĀ·Sh + 1, BĀ·Sh, rv ), *BĀ·Sh = bgĀ·Ch, BĀ·Sh[ ++rv ] = 0, _Sh( BĀ·Sh );  }
  if ( Err ) {  Sh( L"Write Failed with: %d\n\n%c%S", Err, bgĀ·Ch, BĀ·Send );  Ret( 200 )  }
  Listen:  int ErrSrv ;  PĀ·In = EĀ·In = BĀ·In, *EĀ·In = ChĀ·EĀ·In = szII = 0, EĀ­3Ā·Was = -1 ;  
  srvItem:  IĀ·LnĀ·PP = Ln.PP - Ln.BB + 1 ;  imapXtra:
  if ( !Served() ) Ret( 300 )
  if ( !( Protocol == IMAP && !( _EqiN( __LnP( PĀ·In ), ".__ BAD", 7 ) || _EqiN( __LnP( PĀ·In ), ".__ NO", 6 ) )
    || Protocol == SMTP 
       && ( rEq("22") || rEq("23") || rEq("25") || rEq("33") || rEq("35") )
    || Protocol == NNTP
       && ( rEq("10") || rEq("20") || rEq("21") || rEq("22") || rEq("24") || rEq("28") || rEq("34") || rEq("38") )))
  { ErrSrv = _AtoI( __LnP( PĀ·In ) );
    if ( ErrSrv >= 400 && Arts ) {  cntĀ·Dead ++ ;  if ( --cntĀ·ArtsToGo <= 0 )  Ret( 0 )
      ChĀ»( uc0, PĀ·In ), PĀ·In = _PĀ» + CR¦LF + CR&LF ;  goto  srvItem ;  }
    Sh( !ErrSrv ? ( ErrSrv = -400, L"ā— Server said Āæ Āæ :" ) : L"ā— Server said:" );
    ShĀ·PĀ·In:  D = _T - 1 ;  _LnP P = PĀ·In ;
    While( Ch && P - PĀ·In < 120 ) if ( Ch == 13 ) *++D = '\\', *++D = 'r' ; else if ( Ch == 10 ) *++D = '\\', *++D = 'n' ;
    else if ( Ch == 9 ) *++D = '\\', *++D = 't' ; else if ( Ch < 32 ) *++D = L'ā–€' ; else *++D = Ch ;
    *++D = 0, Sh( L"Ā«%sĀ»", _T );  Ret( -ErrSrv )  }

  if ( Arts ) {  __int64 ShouldĀ·Be = anumBot + cntĀ·ArtsToGo + Base - 1, Is = _AtoI( __LnP( PĀ·In + 4 ) );
    if ( !( !Is && _EqN( __LnP( PĀ·In + 3 ), " 0 <", 4 ) ) && Is != ShouldĀ·Be ) {   //  220 0 <303c4
      Sh( L"ā— Expected: %s, Got: %s  @:", ā€ž( ShouldĀ·Be ), ā€ž( Is )), ErrSrv = -500 ; goto  ShĀ·PĀ·In ;  } }

  Sh( L"%.*S", & ChĀ»( uc0, PĀ·In ) - PĀ·In, PĀ·In );
  int imapXtra = Expecting && Protocol == IMAP && ( rEq("* ") || rEq("+ ") );
  if ( !imapXtra && Expecting == 1 )  Ret( 0 )
  if ( Expecting && !imapXtra ) Expecting-- ;
  PĀ·In = _PĀ» + CR¦LF + CR&LF ;

  if ( Expecting ) {  if ( imapXtra ) goto  imapXtra ; goto  srvItem ;  }
  if ( Arts || anArt || eMailĀ·Down )  *infĀ·CharSet = 0 ;  recĀ·CharSet.uiInternetEncoding = CP_UTF8 ;
  if ( !GotĀ·__() )  Ret( 600 )   if ( !Arts )  Ret( 0 )
  if ( !( IĀ·LnĀ·PP = reSh(0) ) )  Ret( 700 )  nGot ++, IncLs( IĀ·LnĀ·PP, 0 );
  if ( nReq ) {
    double  SoFar = Secs - Mark
    , Percent = double( nReq - ( Base + cntĀ·ArtsToGo ) ) / nReq
    , Tot = SoFar / Percent, Remaining = Tot - SoFar ;
    Str( BĀ·Sh, L" %s", !Percent ? L"" : Ago( Remaining / _24_60_60 ) ), ShTaskBar;  }
  if ( --cntĀ·ArtsToGo <= 0 )  Ret( 0 )  goto  srvItem ;
  ResetRtn:  rv = !ErrDia && anArt && Decode;
  MsgĀ·Up = aHdr = anArt = Arts = Expecting = eMailĀ·Down = aPage = Decode = Asking = 0 ;
  if ( rv )  IĀ·LnĀ·PP = reSh(1);   return !ErrDia ;  }
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads