Parse string C
I'm trying to get开发者_高级运维 the body text from an email , but i don't know how. The body is separated with a space from header.Could you give me some examples ?
Thanks.
A message looks like this ( with header and body):
From username@localhost Fri May 13 12:28:30 2010
Return-Path: <username@localhost>
X-Original-To: recipe@localhost
Delivered-To: recipe@localhost
Received: from cristi?localhost (localhost [127.0.0.1])
by Notebook (Postfix) with SMTP id 50F6F809E0
for <test@localhost>; Fri, 13 May 2010 12:28:30 +0300 (EEST)
Message-Id: <20110513092830.50F6F809E0@Cristi-Notebook>
Date: Fri, 13 May 2010 12:28:30 +0300 (EEST)
From: username@localhost
To: undisclosed-recipients:;
Text Body
.
So far :
while ( buffer_recieved[begin]){
if ( buffer_recieved[begin] == '\r' && buffer_recieved[begin+1] == '\n' ) {
body[end++]=buffer_recieved[begin];
}
begin++;
}
body[end]=0;
Find the POP RFC. Read the spec.
I've not read POP, but I've read SMTP. In SMTP I think I recall the header ends in "\r\n \r\n" and the body likewise. Maybe it's the same for POP.
You're making this too complicated. Your body starts if(strnstr(buffer, "\r\n\r\n", sizeof(buffer)) != NULL)
. Now you only need to code the 4 exceptions that exist where the buffer splits within the "\r\n\r\n" sequence.
Another approach is to do line reads till you read "\r\n", then go to buffers for the body. This is preferable because you can store header values more easily and the overhead from doing line reads vs larger buffers is negligible.
If I understood correctly, the body is separated by a newline, so we can keep a variable which tells us if we already met that newline. In that case copy the text, else check if we've reached it.
bool foundBody = false;
char *bodyBegin = "\r\n\r\n";
int i = 0,j = 0, k = 0;
while(bufferReceived[i]) {
if(foundBody)
body[j++] = bufferReceived[i];
else {
if(bufferReceived[i] == bodyBegin[k])
foundBody = bodyBegin[k++] == '\0';
else
k = 0;
}
i += 1;
}
body[j] = '\0';
精彩评论