summaryrefslogtreecommitdiffstats
path: root/mimelib/nntp.cpp
blob: a89293ae687d9ccc823634fbf68d438d38bb5fef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//=============================================================================
// File:       nntp.cpp
// Contents:   Definitions for DwNntpClient
// Maintainer: Doug Sauder <[email protected]>
// WWW:        http://www.fwb.gulf.net/~dwsauder/mimepp.html
//
// Copyright (c) 1996, 1997 Douglas W. Sauder
// All rights reserved.
//
// IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
// INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
// THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
// HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
// NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
// BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
// SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
//=============================================================================

#define DW_IMPLEMENTATION

#include <mimelib/config.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <mimelib/nntp.h>
#include <config.h>

#define NNTP_PORT 119
#define RECV_BUFFER_SIZE  8192
#define SEND_BUFFER_SIZE  1024

#if defined(DW_DEBUG_NNTP)
#  define DBG_NNTP_STMT(x) x
#else
#  define DBG_NNTP_STMT(x)
#endif


DwNntpClient::DwNntpClient()
{
    mSendBuffer = new char[SEND_BUFFER_SIZE];
    mRecvBuffer = new char[RECV_BUFFER_SIZE];
    mLastChar = -1;
    mLastLastChar = -1;
    mNumRecvBufferChars = 0;
    mRecvBufferPos = 0;
    mReplyCode = 0;
    mObserver = 0;
}


DwNntpClient::~DwNntpClient()
{
    if (mRecvBuffer) {
        delete [] mRecvBuffer;
        mRecvBuffer = 0;
    }
    if (mSendBuffer) {
        delete [] mSendBuffer;
        mSendBuffer = 0;
    }
}


int DwNntpClient::Open(const char* aServer, DwUint16 aPort)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    int err = DwProtocolClient::Open(aServer, aPort);
    if (! err) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


DwObserver* DwNntpClient::SetObserver(DwObserver* aObserver)
{
    DwObserver* obs = mObserver;
    mObserver = aObserver;
    return obs;
}


int DwNntpClient::ReplyCode() const
{
    return mReplyCode;
}


const DwString& DwNntpClient::StatusResponse() const
{
    return mStatusResponse;
}


const DwString& DwNntpClient::TextResponse() const
{
    return mTextResponse;
}


int DwNntpClient::Article(int aArticleNum)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdArticle;
    if (aArticleNum >= 0) {
        snprintf(mSendBuffer, SEND_BUFFER_SIZE, "ARTICLE %d\r\n", aArticleNum);
    }
    else {
        strlcpy(mSendBuffer, "ARTICLE\r\n", SEND_BUFFER_SIZE);
    }
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Article(const char* aMsgId)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdArticle;
    if (!aMsgId || !*aMsgId) {
        // error!
        return mReplyCode;
    }
    strlcpy(mSendBuffer, "ARTICLE ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aMsgId, SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, "\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Head(int aArticleNum)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdHead;
    if (aArticleNum >= 0) {
        snprintf(mSendBuffer, SEND_BUFFER_SIZE, "HEAD %d\r\n", aArticleNum);
    }
    else {
        strlcpy(mSendBuffer, "HEAD\r\n", SEND_BUFFER_SIZE);
    }
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Head(const char* aMsgId)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdHead;
    if (!aMsgId || !*aMsgId) {
        return mReplyCode;
    }
    strlcpy(mSendBuffer, "HEAD ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aMsgId, SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, "\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Body(int articleNum)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdBody;
    if (articleNum >= 0) {
        snprintf(mSendBuffer, SEND_BUFFER_SIZE, "BODY %d\r\n", articleNum);
    }
    else {
        strlcpy(mSendBuffer, "BODY\r\n", SEND_BUFFER_SIZE);
    }
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Body(const char* aMsgId)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdBody;
    if (!aMsgId || !*aMsgId) {
        return mReplyCode;
    }
    strlcpy(mSendBuffer, "BODY ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aMsgId, SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, "\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Stat(int articleNum)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdStat;
    if (articleNum >= 0) {
        snprintf(mSendBuffer, SEND_BUFFER_SIZE, "STAT %d\r\n", articleNum);
    }
    else {
        strlcpy(mSendBuffer, "STAT\r\n", SEND_BUFFER_SIZE);
    }
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


int DwNntpClient::Stat(const char* aMsgId)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdStat;
    if (!aMsgId || !*aMsgId) {
        return mReplyCode;
    }
    strlcpy(mSendBuffer, "STAT ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aMsgId, SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, "\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


int DwNntpClient::Group(const char* aNewsgroupName)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdGroup;
    if (!aNewsgroupName || !*aNewsgroupName) {
        return mReplyCode;
    }
    strlcpy(mSendBuffer, "GROUP ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aNewsgroupName, SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, "\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


int DwNntpClient::Help()
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdHelp;
    strlcpy(mSendBuffer, "HELP\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 1) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Last()
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdLast;
    strlcpy(mSendBuffer, "LAST\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


int DwNntpClient::List()
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdList;
    strlcpy(mSendBuffer, "LIST\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Newgroups(const char* aDate, const char* aTime,
    DwBool aIsGmt, const char* aDistribution)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdNewgroups;
    strlcpy(mSendBuffer, "NEWGROUPS ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aDate, SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, " ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aTime, SEND_BUFFER_SIZE);
    if (aIsGmt) {
        strlcat(mSendBuffer, " GMT", SEND_BUFFER_SIZE);
    }
    if (aDistribution) {
        strlcat(mSendBuffer, " ", SEND_BUFFER_SIZE);
        strlcat(mSendBuffer, aDistribution, SEND_BUFFER_SIZE);
    }
    strlcat(mSendBuffer, "\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Newnews(const char* aNewsgroups, const char* aDate,
    const char* aTime, DwBool aIsGmt, const char* aDistribution)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdNewnews;
    strlcpy(mSendBuffer, "NEWNEWS ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aNewsgroups, SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, " ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aDate, SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, " ", SEND_BUFFER_SIZE);
    strlcat(mSendBuffer, aTime, SEND_BUFFER_SIZE);
    if (aIsGmt) {
        strlcat(mSendBuffer, " GMT", SEND_BUFFER_SIZE);
    }
    if (aDistribution) {
        strlcat(mSendBuffer, " ", SEND_BUFFER_SIZE);
        strlcat(mSendBuffer, aDistribution, SEND_BUFFER_SIZE);
    }
    strlcat(mSendBuffer, "\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
        if (mReplyCode/100%10 == 2) {
            PGetTextResponse();
        }
    }
    return mReplyCode;
}


int DwNntpClient::Next()
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdNext;
    strlcpy(mSendBuffer, "NEXT\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


int DwNntpClient::Post()
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdPost;
    strlcpy(mSendBuffer, "POST\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


int DwNntpClient::Quit()
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdQuit;
    strlcpy(mSendBuffer, "QUIT\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


int DwNntpClient::Slave()
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";
    mLastCommand = kCmdSlave;
    strlcpy(mSendBuffer, "SLAVE\r\n", SEND_BUFFER_SIZE);
    DBG_NNTP_STMT(cout << "C: " << mSendBuffer << flush;)
    int bufferLen = strlen(mSendBuffer);
    int numSent = PSend(mSendBuffer, bufferLen);
    if (numSent == bufferLen) {
        PGeStatusResponse();
    }
    return mReplyCode;
}


int DwNntpClient::SendData(const DwString& aStr)
{
    return SendData(aStr.data(), aStr.length());
}


int DwNntpClient::SendData(const char* aBuf, int aBufLen)
{
    mReplyCode = 0;
    mStatusResponse = mTextResponse = "";

    int pos = 0;
    int len = 0;
    const char* buf = 0;

    int lastLastChar = '\r';
    int lastChar = '\n';

    while (1) {

        len = SEND_BUFFER_SIZE;
        len = (len < aBufLen - pos) ? len : aBufLen - pos;
        if (len == 0) break;

        // Look for CR LF '.'.  If it is found, then we have to copy the buffer
        // and stuff an extra '.'.

        int hasCrLfDot = 0;
        int tLastChar = lastChar;
        int tLastLastChar = lastLastChar;
        for (int i=0; i < len; ++i) {
            int ch = aBuf[pos+i];
            if (tLastLastChar == '\r' && tLastChar == '\n' && ch == '.') {
                hasCrLfDot = 1;
                break;
            }
            tLastLastChar = tLastChar;
            tLastChar = ch;
        }
        if (! hasCrLfDot) {
            lastChar = tLastChar;
            lastLastChar = tLastLastChar;
            buf = &aBuf[pos];
            pos += len;
        }

        // If CR LF '.' was found, copy the chars to a different buffer and stuff
        // the extra '.'.

        else /* (hasCrLfDot) */ {
            tLastChar = lastChar;
            tLastLastChar = lastLastChar;
            int iDst = 0;
            int iSrc = 0;
            // Implementation note: be careful to avoid overrunning the
            // destination buffer when CR LF '.' are the last three characters
            // of the source buffer.
            while (iDst < SEND_BUFFER_SIZE && iSrc < len) {
                int ch = aBuf[pos+iSrc];
                if (tLastLastChar == '\r' && tLastChar == '\n' && ch == '.') {
                    if (iDst == SEND_BUFFER_SIZE-1) {
                        break;
                    }
                    mSendBuffer[iDst++] = '.';
                }
                mSendBuffer[iDst++] = (char) ch;
                ++iSrc;
                tLastLastChar = tLastChar;
                tLastChar = ch;
            }
            lastChar = tLastChar;
            lastLastChar = tLastLastChar;
            len = iDst;
            buf = mSendBuffer;
            pos += iSrc;
        }

        // Send the buffer

        int numSent = PSend(buf, len);
        if (numSent != len) {
            mReplyCode = 0;
            return mReplyCode;
        }
    }

    // Send final '.' CR LF.  If CR LF are not at the end of the buffer, then
    // send a CR LF '.' CR LF.

    if (lastLastChar == '\r' && lastChar == '\n') {
        PSend(".\r\n", 3);
    }
    else {
        PSend("\r\n.\r\n", 5);
    }

    // Get the server's response

    PGeStatusResponse();
    return mReplyCode;
}


void DwNntpClient::PGeStatusResponse()
{
    mReplyCode = 0;
    mStatusResponse = "";
    char* ptr;
    int len;
    int err = PGetLine(&ptr, &len);
    if (! err) {
        mReplyCode = strtol(ptr, NULL, 10);
        mStatusResponse.assign(ptr, len);
        DBG_NNTP_STMT(char buffer[256];)
        DBG_NNTP_STMT(strncpy(buffer, ptr, len);)
        DBG_NNTP_STMT(buffer[len] = 0;)
        DBG_NNTP_STMT(cout << "S: " << buffer;)
    }
}


void DwNntpClient::PGetTextResponse()
{
    mTextResponse = "";

    // Get a line at a time until we get CR LF . CR LF

    while (1) {
        char* ptr;
        int len;
        int err = PGetLine(&ptr, &len);

        // Check for an error

        if (err) {
            mReplyCode = 0;
            return;
        }

        // Check for '.' on a line by itself, which indicates end of multiline
        // response

        if (len >= 3 && ptr[0] == '.' && ptr[1] == '\r' && ptr[2] == '\n') {
            break;
        }

        // Remove '.' at beginning of line

        if (*ptr == '.') ++ptr;

        // If an observer is assigned, notify it.
        // Implementation note: An observer is assumed to fetch the multiline
        // response one line at a time, therefore we assign to the string,
        // rather than append to it.

        if (mObserver) {
            mTextResponse.assign(ptr, len);
            mObserver->Notify();
        }
        else {
            mTextResponse.append(ptr, len);
        }
    }
}


int DwNntpClient::PGetLine(char** aPtr, int* aLen)
{
    // Restore the saved state

    int startPos = mRecvBufferPos;
    int pos = mRecvBufferPos;
    int lastChar = -1;

    // Keep trying until we get a complete line, detect an error, or
    // determine that the connection has been closed

    int isEndOfLineFound = 0;
    while (1) {

        // Search buffer for end of line chars. Stop when we find them or when
        // we exhaust the buffer.

        while (pos < mNumRecvBufferChars) {
            if (lastChar == '\r' && mRecvBuffer[pos] == '\n') {
                isEndOfLineFound = 1;
                ++pos;
                break;
            }
            lastChar = mRecvBuffer[pos];
            ++pos;
        }
        if (isEndOfLineFound) {
            *aPtr = &mRecvBuffer[startPos];
            *aLen = pos - startPos;
            mRecvBufferPos = pos;
            return 0;
        }

        // If the buffer has no room, return without an error; otherwise,
        // replenish the buffer.

        // Implementation note: The standard does not allow long lines,
        // however, that does not mean that they won't occur.  The way
        // this function deals with long lines is to return a full buffer's
        // worth of characters as a line.  The next call to this function
        // will start where this call left off.  In essence, we have
        // *forced* a line break, but without putting in CR LF characters.

        if (startPos == 0 && pos == RECV_BUFFER_SIZE) {
            *aPtr = mRecvBuffer;
            *aLen = RECV_BUFFER_SIZE;
            mRecvBufferPos = pos;
            return 0;
        }
        memmove(mRecvBuffer, &mRecvBuffer[startPos],
            mNumRecvBufferChars-startPos);
        mNumRecvBufferChars -= startPos;
        mRecvBufferPos = mNumRecvBufferChars;
        int bufFreeSpace = RECV_BUFFER_SIZE - mRecvBufferPos;
        int n = PReceive(&mRecvBuffer[mRecvBufferPos], bufFreeSpace);
        if (n == 0) {
            // The connection has been closed or an error occurred
            return -1;
        }
        mNumRecvBufferChars += n;
        startPos = 0;
        pos = mRecvBufferPos;
    }
}