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
|
/* $Id: nntpfetch.C,v 1.3 2004/06/14 00:18:43 mrsam Exp $
**
** Copyright 2003, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "nntpfetch.H"
#include <sstream>
#include <ctype.h>
using namespace std;
mail::nntp::FetchTaskBase::FetchTaskBase(mail::callback *callbackArg,
nntp &myserverArg,
std::string groupNameArg,
size_t msgNumArg,
std::string uidArg,
mail::readMode getTypeArg)
: GroupTask(callbackArg, myserverArg, groupNameArg),
msgNum(msgNumArg),
uid(uidArg),
getType(getTypeArg)
{
}
mail::nntp::FetchTaskBase::~FetchTaskBase()
{
}
void mail::nntp::FetchTaskBase::selectedGroup(msgnum_t estimatedCount,
msgnum_t loArticleCount,
msgnum_t hiArticleCount)
{
response_func= &mail::nntp::FetchTaskBase::processStatusResponse;
if (!myserver->fixGenericMessageNumber(uid, msgNum))
{
fail("Invalid message number.");
return;
}
byteCount=0;
ostringstream o;
switch (getType) {
case mail::readContents:
o << "BODY ";
break;
case mail::readBoth:
o << "ARTICLE ";
break;
default:
o << "HEAD ";
}
o << myserver->index[msgNum].msgNum << "\r\n";
myserver->socketWrite(o.str());
}
void mail::nntp::FetchTaskBase::processGroup(const char *cmd)
{
(this->*response_func)(cmd);
}
void mail::nntp::FetchTaskBase::processStatusResponse(const char *msg)
{
if (msg[0] != '2')
{
ostringstream o;
o << "The following error occured while reading this article:\n"
"\n"
" " << msg << "\n\n";
fetchedText(o.str());
success("Ok");
return;
}
response_func= getType == mail::readHeadersFolded
? &mail::nntp::FetchTaskBase::processFetchFoldedResponse:
&mail::nntp::FetchTaskBase::processFetchResponse;
foldedNewline=false;
}
void mail::nntp::FetchTaskBase::processFetchResponse(const char *cmd)
{
if (strcmp(cmd, "."))
{
if (*cmd == '.')
++cmd;
string s(cmd);
s += "\n";
byteCount += s.size();
if (callbackPtr)
callbackPtr->reportProgress(byteCount, 0, 0, 1);
fetchedText(s);
return;
}
if (callbackPtr)
callbackPtr->reportProgress(byteCount, byteCount, 0, 1);
success("Ok\n");
}
void mail::nntp::FetchTaskBase::processFetchFoldedResponse(const char *cmd)
{
if (strcmp(cmd, "."))
{
if (*cmd == '.')
++cmd;
if (*cmd && isspace((int)(unsigned char)*cmd) &&
foldedNewline)
{
while (*cmd && isspace((int)(unsigned char)*cmd))
cmd++;
string s(" ");
s += cmd;
byteCount += s.size();
if (callbackPtr)
callbackPtr->
reportProgress(byteCount, 0, 0, 1);
fetchedText(s);
}
else
{
string s;
if (foldedNewline)
s += "\n";
foldedNewline=true;
s += cmd;
byteCount += s.size();
if (callbackPtr)
callbackPtr->
reportProgress(byteCount, 0, 0, 1);
fetchedText(s);
}
return;
}
++byteCount;
if (callbackPtr)
callbackPtr->reportProgress(byteCount, byteCount, 0, 1);
fetchedText("\n");
success("Ok");
}
//
// TaskBase is recyled by CacheTask, which also fetches a message's contents,
// but does not have a callback::message, because it goes into a temporary
// file. That's why we separate out callback::message-specific processing
// into a subclass.
//
mail::nntp::FetchTask::FetchTask(callback::message *textCallbackArg,
nntp &myserverArg,
std::string groupNameArg,
size_t msgNumArg,
std::string uidArg,
mail::readMode getType)
: FetchTaskBase(textCallbackArg, myserverArg,
groupNameArg,
msgNumArg,
uidArg,
getType),
textCallback(*textCallbackArg)
{
}
mail::nntp::FetchTask::~FetchTask()
{
}
void mail::nntp::FetchTask::fetchedText(std::string s)
{
textCallback.messageTextCallback(msgNum, s);
}
|