📄 parse.c
字号:
/* * Embedded POP3 Parser * * ./software/ch6/empop3c/parse.c * * mtj@cogitollc.com * */#include <stdio.h>#include <string.h>#include "empop3c.h"/* * parseEntry() * * Given a retrieved mail object and a search string, load into 'dest' * the value that follows the searchstring. For example "To: me" would * load "me" into the dest argument for a searchstring of "To:". * * Returns 0 on success, -1 on failure. * */int parseEntry( mail_t *mail, char *searchString, char *dest ){ int i, index, len; len = strlen(searchString); /* Grab the subject */ for (i = 0 ; i < mail->email_len ; i++) { if (mail->email[i] == searchString[0]) { if (!strncmp(&mail->email[i], searchString, len )) { i += (len + 1); index = 0; while (!((mail->email[i] == 0x0d) && (mail->email[i+1] == 0x0a))) { if (mail->email[i] == 0) i++; else dest[index++] = mail->email[i++]; } dest[index] = 0; break; } } } if (i == mail->email_len) { return -1; } return 0;}/* * findBody() * * Given a retrieved mail object, find the body portion of the email * (identified by the CR LF CR LF -- blank line). A blank line separates * the header from the body of the email. * * Returns 0 on success, -1 on failure. * */int findBody( mail_t *mail ){ int i, result=-1; for (i = 0 ; i < mail->email_len ; i++) { if ((mail->email[i] == 0x0d) && (mail->email[i+1] == 0x0a) && (mail->email[i+2] == 0x0d) && (mail->email[i+3] == 0x0a)) { i+=4; break; } } if (i < mail->email_len) { mail->bodyStart = &mail->email[i]; result = 0; } return result;}/* * fixAddress() * * Given a passed email address, extract the real address from the possible * SMTP <> definition. For example, an address passed as "me <me@home.com>" * will result in the filtered "me@home.com" address. Addresses that are * already in the proper form will not be altered. * * Returns 0 on success, -1 on failure. * */int fixAddress ( char *address ){ int i, j, len; char string[MAX_STRING+1]; len = strlen(address); for (i = 0 ; i < len ; i++) { if (address[i] == '<') break; } if (i++ == len) return 0; j = 0; for ( ; i < len ; i++) { if (address[i] == '>') break; string[j++] = address[i]; } string[j] = 0; strcpy(address, string); return 0;}/* * pop3cParse() * * This function performs some preliminary parsing on the retrieved email * object. The following actions are performed: * * The Subject of the email is parsed into the subject element of mail. * * The From of the email is parsed into the sender element of mail. * * The To of the email is parsed into the recipient element of mail. * * The start of the body of the email is loaded into the bodyStart * element of the mail object. * * Returns 0 on success, -1 on failure. * */int pop3cParse ( mail_t *mail ){ int result; result = parseEntry( mail, "Subject:", mail->subject ); if (result < 0) return result; result = parseEntry( mail, "From:", mail->sender ); if (result < 0) return result; fixAddress( mail->sender ); result = parseEntry( mail, "To:", mail->recipient ); if (result < 0) return result; fixAddress( mail->recipient ); result = findBody( mail ); printf("body is \n%s\n", mail->bodyStart); return result;}/* * Copyright (c) 2002 Charles River Media. All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, is hereby granted without fee provided * that the following conditions are met: * * 1. Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * 3. Neither the name of Charles River Media nor the names of * its contributors may be used to endorse or promote products * derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY CHARLES RIVER MEDIA AND CONTRIBUTERS * 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CHARLES * RIVER MEDIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARAY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -