improved tests to check for mail
This commit is contained in:
parent
4f6d15b821
commit
2df77de886
@ -1,5 +1,5 @@
|
|||||||
#ifndef lint
|
#ifndef lint
|
||||||
static char rcsid[] = "@(#$Id: idfile.cpp,v 1.4 2006-01-23 13:32:28 dockes Exp $ (C) 2005 J.F.Dockes";
|
static char rcsid[] = "@(#$Id: idfile.cpp,v 1.5 2006-12-02 07:32:13 dockes Exp $ (C) 2005 J.F.Dockes";
|
||||||
#endif
|
#endif
|
||||||
/*
|
/*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
@ -31,6 +31,13 @@ static char rcsid[] = "@(#$Id: idfile.cpp,v 1.4 2006-01-23 13:32:28 dockes Exp $
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
#endif /* NO_NAMESPACES */
|
#endif /* NO_NAMESPACES */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This code is currently ONLY used to identify mbox and mail message files
|
||||||
|
* which are badly handled by standard mime type identifiers
|
||||||
|
* There is a very old (circa 1990) mbox format using blocks of ^A (0x01) chars
|
||||||
|
* to separate messages, that we don't recognize currently
|
||||||
|
*/
|
||||||
|
|
||||||
std::list<string> idFileAllTypes()
|
std::list<string> idFileAllTypes()
|
||||||
{
|
{
|
||||||
std::list<string> lst;
|
std::list<string> lst;
|
||||||
@ -41,8 +48,9 @@ std::list<string> idFileAllTypes()
|
|||||||
|
|
||||||
// Mail headers we compare to:
|
// Mail headers we compare to:
|
||||||
static const char *mailhs[] = {"From: ", "Received: ", "Message-Id: ", "To: ",
|
static const char *mailhs[] = {"From: ", "Received: ", "Message-Id: ", "To: ",
|
||||||
"Date: ", "Subject: ", "Status: "};
|
"Date: ", "Subject: ", "Status: ",
|
||||||
static const int mailhsl[] = {6, 10, 12, 4, 6, 9, 8};
|
"In-Reply-To: "};
|
||||||
|
static const int mailhsl[] = {6, 10, 12, 4, 6, 9, 8, 13};
|
||||||
static const int nmh = sizeof(mailhs) / sizeof(char *);
|
static const int nmh = sizeof(mailhs) / sizeof(char *);
|
||||||
|
|
||||||
const int wantnhead = 3;
|
const int wantnhead = 3;
|
||||||
@ -57,12 +65,14 @@ string idFile(const char *fn)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool line1HasFrom = false;
|
bool line1HasFrom = false;
|
||||||
|
bool gotnonempty = false;
|
||||||
int lookslikemail = 0;
|
int lookslikemail = 0;
|
||||||
|
|
||||||
// emacs VM sometimes inserts very long lines with continuations or
|
// emacs VM sometimes inserts very long lines with continuations or
|
||||||
// not (for folder information). This forces us to look at many
|
// not (for folder information). This forces us to look at many
|
||||||
// lines and long ones
|
// lines and long ones
|
||||||
for (int lnum = 1; lnum < 200; lnum++) {
|
int lnum = 1;
|
||||||
|
for (int loop = 1; loop < 200; loop++, lnum++) {
|
||||||
|
|
||||||
#define LL 1024
|
#define LL 1024
|
||||||
char cline[LL+1];
|
char cline[LL+1];
|
||||||
@ -77,13 +87,45 @@ string idFile(const char *fn)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGDEB2(("idfile: lnum %d : [%s]\n", lnum, cline));
|
// gcount includes the \n
|
||||||
// Check for a few things that can't be found in a mail file,
|
int ll = input.gcount() - 1;
|
||||||
// (optimization to get a quick negative
|
if (ll > 0)
|
||||||
|
gotnonempty = true;
|
||||||
|
|
||||||
// Lines must begin with whitespace or have a colon in the
|
LOGDEB2(("idfile: lnum %d ll %d: [%s]\n", lnum, ll, cline));
|
||||||
// first 50 chars (hope no one comes up with a longer header
|
|
||||||
// name !
|
// Check for a few things that can't be found in a mail file,
|
||||||
|
// (optimization to get a quick negative)
|
||||||
|
|
||||||
|
// Empty lines
|
||||||
|
if (ll <= 0) {
|
||||||
|
// Accept a few empty lines at the beginning of the file,
|
||||||
|
// otherwise this is the end of headers
|
||||||
|
if (gotnonempty || lnum > 10) {
|
||||||
|
LOGDEB2(("Got empty line\n"));
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// Don't increment the line counter for initial empty lines.
|
||||||
|
lnum--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// emacs vm can insert VERY long header lines.
|
||||||
|
if (ll > 800) {
|
||||||
|
LOGDEB2(("idFile: Line too long\n"));
|
||||||
|
return string("");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for mbox 'From ' line
|
||||||
|
if (lnum == 1 && !strncmp("From ", cline, 5)) {
|
||||||
|
line1HasFrom = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Except for a possible first line with 'From ', lines must
|
||||||
|
// begin with whitespace or have a colon
|
||||||
|
// (hope no one comes up with a longer header name !
|
||||||
if (!isspace(cline[0])) {
|
if (!isspace(cline[0])) {
|
||||||
char *cp = strchr(cline, ':');
|
char *cp = strchr(cline, ':');
|
||||||
if (cp == 0 || (cp - cline) > 70) {
|
if (cp == 0 || (cp - cline) > 70) {
|
||||||
@ -92,18 +134,7 @@ string idFile(const char *fn)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ll = strlen(cline);
|
// Compare to known headers
|
||||||
if (ll > 1000) {
|
|
||||||
LOGDEB2(("idFile: Line too long\n"));
|
|
||||||
return string("");
|
|
||||||
}
|
|
||||||
if (lnum == 1) {
|
|
||||||
if (!strncmp("From ", cline, 5)) {
|
|
||||||
line1HasFrom = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < nmh; i++) {
|
for (int i = 0; i < nmh; i++) {
|
||||||
if (!strncasecmp(mailhs[i], cline, mailhsl[i])) {
|
if (!strncasecmp(mailhs[i], cline, mailhsl[i])) {
|
||||||
//fprintf(stderr, "Got [%s]\n", mailhs[i]);
|
//fprintf(stderr, "Got [%s]\n", mailhs[i]);
|
||||||
@ -139,14 +170,16 @@ using namespace std;
|
|||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if (argc != 2) {
|
if (argc < 2) {
|
||||||
cerr << "Usage: idfile filename" << endl;
|
cerr << "Usage: idfile filename" << endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
DebugLog::getdbl()->setloglevel(DEBDEB1);
|
DebugLog::getdbl()->setloglevel(DEBDEB1);
|
||||||
DebugLog::setfilename("stderr");
|
DebugLog::setfilename("stderr");
|
||||||
string mime = idFile(argv[1]);
|
for (int i = 1; i < argc; i++) {
|
||||||
cout << argv[1] << " : " << mime << endl;
|
string mime = idFile(argv[i]);
|
||||||
|
cout << argv[i] << " : " << mime << endl;
|
||||||
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user