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
|
/*
** Copyright 2003, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "nntppost.H"
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <cstring>
using namespace std;
mail::nntp::PostTask::PostTask(callback *callbackArg, nntp &myserverArg,
FILE *msgArg)
: LoggedInTask(callbackArg, myserverArg), msg(msgArg),
myPumper(NULL)
{
}
mail::nntp::PostTask::~PostTask()
{
if (myPumper)
{
myPumper->writebuffer += "\r\n.\r\n";
myPumper->me=NULL;
}
if (msg)
fclose(msg);
}
void mail::nntp::PostTask::loggedIn()
{
struct stat stat_buf;
if (fseek(msg, 0L, SEEK_SET) < 0 || fstat(fileno(msg), &stat_buf) < 0)
{
fail(strerror(errno));
return;
}
tot_count=stat_buf.st_size;
byte_count=0;
response_func= &mail::nntp::PostTask::doPostStatus;
myserver->socketWrite("POST\r\n");
}
void mail::nntp::PostTask::processLine(const char *message)
{
(this->*response_func)(message);
}
mail::nntp::PostTask::pump::pump(PostTask *p) : newLine(true), me(p)
{
}
mail::nntp::PostTask::pump::~pump()
{
if (me)
me->myPumper=NULL;
}
bool mail::nntp::PostTask::pump::fillWriteBuffer()
{
if (!me)
return false;
char buffer[BUFSIZ];
int n=fread(buffer, 1, sizeof(buffer), me->msg);
if (n <= 0)
{
if (!newLine)
writebuffer += "\r\n";
writebuffer += ".\r\n";
me->myPumper=NULL;
me=NULL;
return true;
}
// Copy the read chunk into the writebuffer, convert NLs to CRNLs,
// and dot-stuffing.
char *b=buffer, *e=b + n, *c=b;
while (b != e)
{
if (newLine && *b == '.') // Leading dot, double it.
{
writebuffer.insert(writebuffer.end(), c, b+1);
c=b; // Net effect is the dot doubled.
}
if ((newLine= *b == '\n') != 0)
{
if (c != b)
writebuffer.insert(writebuffer.end(), c, b);
writebuffer += "\r";
c=b;
}
b++;
}
if (c != b)
writebuffer.insert(writebuffer.end(), c, b);
me->byte_count += n;
if (me->tot_count < me->byte_count)
me->tot_count=me->byte_count;
me->callbackPtr->reportProgress(me->byte_count, me->tot_count, 0, 1);
return true;
}
void mail::nntp::PostTask::doPostStatus(const char *resp)
{
if (resp[0] != '3')
{
fail(resp);
return;
}
response_func= &mail::nntp::PostTask::doPost;
pump *p=new pump(this);
if (p)
try {
myserver->socketWrite(p);
myPumper=p;
return;
} catch (...) {
delete p;
}
myserver->socketWrite(".\r\n");
}
void mail::nntp::PostTask::doPost(const char *msg)
{
if (myPumper)
{
myPumper->writebuffer += "\r\n.\r\n";
myPumper->me=NULL;
}
myPumper=NULL;
switch (msg[0]) {
case '2':
case '1':
success(msg);
break;
default:
fail(msg);
}
}
|