mail handling 1st working version
This commit is contained in:
@@ -33,6 +33,8 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <istream>
|
||||
|
||||
namespace Binc {
|
||||
|
||||
class MimeInputSource {
|
||||
@@ -40,9 +42,10 @@ namespace Binc {
|
||||
inline MimeInputSource(int fd, unsigned int start = 0);
|
||||
virtual inline ~MimeInputSource(void);
|
||||
|
||||
virtual inline bool fillInputBuffer(void);
|
||||
virtual inline size_t fillRaw(char *raw, size_t nbytes);
|
||||
virtual inline void reset(void);
|
||||
|
||||
virtual inline bool fillInputBuffer(void);
|
||||
inline void seek(unsigned int offset);
|
||||
inline bool getChar(char *c);
|
||||
inline void ungetChar(void);
|
||||
@@ -77,10 +80,15 @@ namespace Binc {
|
||||
{
|
||||
}
|
||||
|
||||
inline size_t MimeInputSource::fillRaw(char *raw, size_t nbytes)
|
||||
{
|
||||
return read(fd, raw, nbytes);
|
||||
}
|
||||
|
||||
inline bool MimeInputSource::fillInputBuffer(void)
|
||||
{
|
||||
char raw[4096];
|
||||
ssize_t nbytes = read(fd, raw, sizeof(raw));
|
||||
ssize_t nbytes = fillRaw(raw, 4096);
|
||||
if (nbytes <= 0) {
|
||||
// FIXME: If ferror(crlffile) we should log this.
|
||||
return false;
|
||||
@@ -159,8 +167,53 @@ namespace Binc {
|
||||
{
|
||||
return offset;
|
||||
}
|
||||
|
||||
///////////////////////////////////
|
||||
class MimeInputSourceStream : public MimeInputSource {
|
||||
public:
|
||||
inline MimeInputSourceStream(istream& s, unsigned int start = 0);
|
||||
virtual inline size_t fillRaw(char *raw, size_t nb);
|
||||
virtual inline void reset(void);
|
||||
private:
|
||||
istream& s;
|
||||
};
|
||||
|
||||
inline MimeInputSourceStream::MimeInputSourceStream(istream& si,
|
||||
unsigned int start)
|
||||
: MimeInputSource(-1, start), s(si)
|
||||
{
|
||||
}
|
||||
|
||||
inline size_t MimeInputSourceStream::fillRaw(char *raw, size_t nb)
|
||||
{
|
||||
// Why can't streams tell how many characters were actually read
|
||||
// when hitting eof ?
|
||||
std::streampos st = s.tellg();
|
||||
s.seekg(0, ios_base::end);
|
||||
std::streampos lst = s.tellg();
|
||||
s.seekg(st);
|
||||
size_t nbytes = lst - st;
|
||||
if (nbytes > nb) {
|
||||
nbytes = nb;
|
||||
}
|
||||
if (nbytes <= 0) {
|
||||
return (size_t)-1;
|
||||
}
|
||||
|
||||
s.read(raw, nbytes);
|
||||
return nbytes;
|
||||
}
|
||||
|
||||
inline void MimeInputSourceStream::reset(void)
|
||||
{
|
||||
MimeInputSource::reset();
|
||||
s.seekg(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
extern Binc::MimeInputSource *mimeSource;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -80,6 +80,35 @@ void Binc::MimeDocument::parseFull(int fd) const
|
||||
size = mimeSource->getOffset();
|
||||
}
|
||||
|
||||
void Binc::MimeDocument::parseFull(istream& s) const
|
||||
{
|
||||
if (allIsParsed)
|
||||
return;
|
||||
|
||||
allIsParsed = true;
|
||||
|
||||
delete mimeSource;
|
||||
mimeSource = new MimeInputSourceStream(s);
|
||||
|
||||
headerstartoffsetcrlf = 0;
|
||||
headerlength = 0;
|
||||
bodystartoffsetcrlf = 0;
|
||||
bodylength = 0;
|
||||
size = 0;
|
||||
messagerfc822 = false;
|
||||
multipart = false;
|
||||
|
||||
int bsize = 0;
|
||||
string bound;
|
||||
MimePart::parseFull(bound, bsize);
|
||||
|
||||
// eat any trailing junk to get the correct size
|
||||
char c;
|
||||
while (mimeSource->getChar(&c));
|
||||
|
||||
size = mimeSource->getOffset();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
static bool parseOneHeaderLine(Binc::Header *header, unsigned int *nlines)
|
||||
{
|
||||
|
||||
@@ -73,6 +73,29 @@ void Binc::MimeDocument::parseOnlyHeader(int fd) const
|
||||
MimePart::parseOnlyHeader("");
|
||||
}
|
||||
|
||||
void Binc::MimeDocument::parseOnlyHeader(istream& s) const
|
||||
{
|
||||
if (allIsParsed || headerIsParsed)
|
||||
return;
|
||||
|
||||
headerIsParsed = true;
|
||||
|
||||
delete mimeSource;
|
||||
mimeSource = new MimeInputSourceStream(s);
|
||||
|
||||
headerstartoffsetcrlf = 0;
|
||||
headerlength = 0;
|
||||
bodystartoffsetcrlf = 0;
|
||||
bodylength = 0;
|
||||
messagerfc822 = false;
|
||||
multipart = false;
|
||||
|
||||
nlines = 0;
|
||||
nbodylines = 0;
|
||||
|
||||
MimePart::parseOnlyHeader("");
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
int Binc::MimePart::parseOnlyHeader(const string &toboundary) const
|
||||
{
|
||||
|
||||
@@ -77,11 +77,18 @@ void Binc::MimePart::getBody(int fd, string &s,
|
||||
unsigned int startoffset,
|
||||
unsigned int length) const
|
||||
{
|
||||
|
||||
if (!mimeSource || mimeSource->getFileDescriptor() != fd) {
|
||||
delete mimeSource;
|
||||
mimeSource = new MimeInputSource(fd);
|
||||
}
|
||||
getBody(s, startoffset, length);
|
||||
}
|
||||
|
||||
void Binc::MimePart::getBody(string &s,
|
||||
unsigned int startoffset,
|
||||
unsigned int length) const
|
||||
{
|
||||
mimeSource->reset();
|
||||
mimeSource->seek(bodystartoffsetcrlf + startoffset);
|
||||
|
||||
|
||||
@@ -108,6 +108,7 @@ namespace Binc {
|
||||
|
||||
void printBody(int fd, Binc::IODevice &output, unsigned int startoffset, unsigned int length) const;
|
||||
void getBody(int fd, std::string& s, unsigned int startoffset, unsigned int length) const;
|
||||
void getBody(std::string& s, unsigned int startoffset, unsigned int length) const;
|
||||
void printHeader(int fd, Binc::IODevice &output, std::vector<std::string> headers, bool includeheaders, unsigned int startoffset, unsigned int length, std::string &storage) const;
|
||||
void printDoc(int fd, Binc::IODevice &output, unsigned int startoffset, unsigned int length) const;
|
||||
virtual void clear(void) const;
|
||||
@@ -129,6 +130,8 @@ namespace Binc {
|
||||
public:
|
||||
void parseOnlyHeader(int fd) const;
|
||||
void parseFull(int fd) const;
|
||||
void parseOnlyHeader(std::istream& s) const;
|
||||
void parseFull(std::istream& s) const;
|
||||
void clear(void) const;
|
||||
|
||||
inline bool isHeaderParsed(void) { return headerIsParsed; }
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef lint
|
||||
static char rcsid [] = "@(#$Id: trbinc.cc,v 1.1 2005-03-25 09:40:27 dockes Exp $ (C) 1994 CDKIT";
|
||||
static char rcsid [] = "@(#$Id: trbinc.cc,v 1.2 2005-03-31 10:04:07 dockes Exp $ (C) 1994 CDKIT";
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -9,6 +9,10 @@ static char rcsid [] = "@(#$Id: trbinc.cc,v 1.1 2005-03-25 09:40:27 dockes Exp $
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "mime.h"
|
||||
|
||||
static char *thisprog;
|
||||
@@ -68,7 +72,27 @@ int main(int argc, char **argv)
|
||||
exit(1);
|
||||
}
|
||||
Binc::MimeDocument doc;
|
||||
|
||||
#if 0
|
||||
doc.parseFull(fd);
|
||||
#else
|
||||
char *cp;
|
||||
int size = lseek(fd, 0, SEEK_END);
|
||||
lseek(fd, 0, 0);
|
||||
fprintf(stderr, "Size: %d\n", size);
|
||||
cp = (char *)malloc(size);
|
||||
if (cp==0) {
|
||||
fprintf(stderr, "Malloc %d failed\n", size);
|
||||
exit(1);
|
||||
}
|
||||
int n;
|
||||
if ((n=read(fd, cp, size)) != size) {
|
||||
fprintf(stderr, "Read failed: requested %d, got %d\n", size, n);
|
||||
exit(1);
|
||||
}
|
||||
std::stringstream s(string(cp, size), ios::in);
|
||||
doc.parseFull(s);
|
||||
#endif
|
||||
|
||||
if (!doc.isHeaderParsed() && !doc.isAllParsed()) {
|
||||
fprintf(stderr, "Parse error\n");
|
||||
|
||||
Reference in New Issue
Block a user