⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailbox.c

📁 LINUX下的收发E-MAIL的程序
💻 C
字号:

#include "mailbox.h"

char emlXMailer[] = "HUC Mail 0.1";

unsigned int *emlMailpos;


char *eml_mail_get_subject(char *message)
{
    static char buffer[100];
    char ch;
    int index, i;

    index = find_string(message, "Subject: ");
    if (index == -1)
	return NULL;

// advance to the start of the subject 
    index += strlen("Subject: ");

    i = 0;
    ch = message[index];
    memset(buffer, 0, sizeof(buffer));
    while (ch != '\r' && ch != '\n' && i < sizeof(buffer) - 1) {
	buffer[i] = ch;
	index++;
	i++;
	ch = message[index];
    }
    buffer[i] = '\0';

    trim_whtspc(buffer);
    if (buffer[0] != '\0')
	parse_8bit(buffer);
    return strdup(buffer);	//subject;
}


char *eml_mail_get_from(char *message)
{
    static char buffer[64];
    char ch;
    int index, i;

    index = find_string(message, "From: ");

    if (index == -1)
	return NULL;

    // advance to the start of From: 

    index += strlen("From: ");

    i = 0;
    ch = message[index];
    memset(buffer, 0, sizeof(buffer));
    while (ch != '\r' && ch != '\n' && i < sizeof(buffer) - 1) {
	buffer[i] = ch;
	index++;
	i++;
	ch = message[index];
    }
    buffer[i] = '\0';

    trim_whtspc(buffer);

    if (buffer[0] != '\0')
	parse_8bit(buffer);
    return strdup(buffer);
}


char *eml_mail_get_to(char *message)
{
    static char buffer[50];
    char ch;
    int index, i;

    index = find_string(message, "From: ");
    index = index + find_string(message + index, "To: ");

    // advance to the start of To: 
    index = index + strlen("To: ");

    i = 0;
    ch = message[index];
    memset(buffer, 0, sizeof(buffer));
    while (ch != '\r' && ch != '\n' && i < sizeof(buffer) - 1) {
	buffer[i] = ch;
	index++;
	i++;
	ch = message[index];
    }
    buffer[i] = '\0';

    trim_whtspc(buffer);
    if (buffer[0] != '\0')
	parse_8bit(buffer);

    return strdup(buffer);	//to;
}


char *eml_mail_get_replyto(char *message)
{
    static char buffer[52];
    char ch;
    int index, i;

    index = find_string(message, "\nReply-To: ");
    if (index == -1)
	return NULL;

    // advance to the start of Reply-To: 
    index += strlen("\nReply-To: ");

    i = 0;
    ch = message[index];
    memset(buffer, 0, sizeof(buffer));
    while (ch != '\r' && ch != '\n' && i < sizeof(buffer) - 1) {
	buffer[i] = ch;
	index++;
	i++;
	ch = message[index];
    }
    buffer[i] = '\0';
    trim_whtspc(buffer);
    return strdup(buffer);
}


char *eml_mail_get_messageid(char *message)
{
    static char buffer[128];
    char ch;
    int index, i;


    index = find_string(message, "Message-Id: ");

    if (index == -1) {
	index = find_string(message, "Message-ID: ");
    }

    if (index == -1)
	return NULL;

    // advance to the start of To: 
    index += strlen("Message-Id: ");

    i = 0;
    ch = message[index];
    memset(buffer, 0, sizeof(buffer));
    while (ch != '\r' && ch != '\n' && i < sizeof(buffer) - 1) {
	buffer[i] = ch;
	index++;
	i++;
	ch = message[index];
    }
    buffer[i] = '\0';

    trim_whtspc(buffer);

    return strdup(buffer);	//messageid;
}


char *eml_mail_get_date(char *message)
{
    static char buffer[40];
    char ch;
    int index, i;

    index = find_string(message, "Date: ");
    if (index == -1)
	return NULL;

    // advance to the start of the date 
    index += strlen("Date: ");

    i = 0;
    ch = message[index];
    memset(buffer, 0, sizeof(buffer));
    while (ch != '\r' && ch != '\n' && i < sizeof(buffer) - 1) {
	buffer[i] = ch;
	index++;
	i++;
	ch = message[index];
    }
    buffer[i] = '\0';

    trim_whtspc(buffer);

    return strdup(buffer);
}


char *eml_mail_get_EncodeMethod(char *message)
{
    static char buffer[30];
    char ch, *encodeMethod = NULL;
    int index, i;

    index = find_string(message, "Content-Transfer-Encoding: ");
    if (index == -1)
	return NULL;

    // advance to the start of the xmailer 
    index += strlen("Content-Transfer-Encoding: ");

    i = 0;
    ch = message[index];
    memset(buffer, 0, sizeof(buffer));
    while (ch != '\r' && ch != '\n' && i < sizeof(buffer) - 1) {
	buffer[i] = ch;
	index++;
	i++;
	ch = message[index];
    }
    buffer[i] = '\0';
    trim_whtspc(buffer);
    encodeMethod = buffer;

    return encodeMethod;
}


char *eml_mail_get_xmailer(char *message)
{
    static char buffer[40];
    char ch, *xmailer;
    int index, i;

    index = find_string(message, "X-Mailer: ");
    if (index == -1)
	return NULL;

    // advance to the start of the xmailer 
    index += strlen("X-Mailer: ");

    i = 0;
    ch = message[index];
    memset(buffer, 0, sizeof(buffer));
    while (ch != '\r' && ch != '\n' && i < sizeof(buffer) - 1) {
	buffer[i] = ch;
	index++;
	i++;
	ch = message[index];
    }
    buffer[i] = '\0';
    trim_whtspc(buffer);
    xmailer = buffer;
    return xmailer;
}


char *eml_mail_get_header_field(char *message, char *field)
{
    static char buffer[512];
    char *header, *index, temp_line[512];
    char *mesg_header;
    int i, done, first;

    index = strstr(message, "\n\n");
    if (index == NULL)
	return (char *) NULL;

    index += 2;
    // now lets make a copy of the header info 
    mesg_header = g_malloc0(index - message + 1);
    strncpy(mesg_header, message, index - message);
    mesg_header[index - message] = '\0';

    index = strstrcase(mesg_header, field);
    if (index == NULL)
	return NULL;

    // NULL out out buffer 
    memset(buffer, 0, 512);

    // advance to the start of the header field 
    index += strlen(field);

    done = 0;
    first = 1;
    // we'll get data until the first char in the line (except the first line) 
    // is not a tab (indicates a new header field) 
    while (done == 0) {
	i = 0;
	while (*index != '\n') {
	    temp_line[i] = *index;
	    i++;
	    index++;
	}
	temp_line[i] = '\0';

	if (!first) {
	    if (temp_line[0] != '\t' && temp_line[0] != ' ')
		done = 1;
	}

	if (!done) {
	    strncat(buffer, temp_line, sizeof(buffer) - 1);
	    index++;
	    first = 0;
	}
    }

    trim_whtspc(buffer);

    if (!strcmp(field, "Subject:") || !strcmp(field, "From:"))
	parse_8bit(buffer);

    header = buffer;

    free(mesg_header);

    return header;
}


int get_header_len(char *mesg)
{
    char *start;
    // advance to the point where there are at least 2x end-lines
    // which should mark the end of the header info and the beginning
    // of the body of the message. 
    for (start = mesg; *start != '\0'; start++)
	if (*start == '\n' && *(start - 1) == '\n')
	    break;
    return (start - mesg);
}


char *get_trimmed_header(char *mesg, int trim_level, int hdrlen)
{
    char *hdrinfo, *hdrstripped;
    char *hdrptr, *fieldptr;
    char showfields[6][16] =
	{ "Date:", "\nTo:", "\nFrom:", "\nReply-To:", "\nSubject:", "\0" };
    int end = FALSE;
    int i;

    hdrinfo = g_malloc0(hdrlen + 1);
    strncpy(hdrinfo, mesg, hdrlen);
    if (trim_level == 2)
	return hdrinfo;

    hdrstripped = g_malloc0(hdrlen + 1);
    i = 0;
    hdrptr = hdrstripped;
    while (*showfields[i] != '\0') {
	if ((fieldptr = strstrcase(hdrinfo, showfields[i])) != NULL) {
	    end = FALSE;
	    *hdrptr = *fieldptr;
	    hdrptr++;
	    fieldptr++;
	    while (!end) {
		for (; *fieldptr != '\n' && *fieldptr != '\0';
		     hdrptr++, fieldptr++)
		    *hdrptr = *fieldptr;
		end = TRUE;
		if (*fieldptr == '\n'
		    && (*(fieldptr + 1) == ' '
			|| *(fieldptr + 1) == '\t')) {
		    end = FALSE;	// the field was wrapped 
		    *hdrptr = '\n';
		    fieldptr++;
		    hdrptr++;
		}
	    }
	    *hdrptr = '\n';
	}
	i++;
    }
    *hdrptr = '\0';
    free(hdrinfo);
    hdrinfo = strdup(hdrstripped);
    free(hdrstripped);

    parse_8bit(hdrinfo);

    return (hdrinfo);
}


void trim_header(char *mesg, int trim_level)
{
    char *start, *ptr, *ptr_mesg;
    char *hdrstripped;
    int hdrlen;

    start = mesg;
    hdrlen = get_header_len(mesg);
    if (trim_level == 2)
	start += hdrlen;

    if (trim_level == 1)	// show partial header only 
    {
	start += hdrlen;	//added by me;
	hdrstripped = get_trimmed_header(mesg, trim_level, hdrlen);
	start -= strlen(hdrstripped);
	strncpy(start, hdrstripped, strlen(hdrstripped));
	free(hdrstripped);
    }
    // advance to the actual beginning of the message (gets rid of any
    // extra line feeds that might be between the header and the actual
    // message). 
    for (; *start != '\0'; start++)
	if (*start != '\n')
	    break;

    for (ptr_mesg = mesg, ptr = start; *ptr != '\0'; ptr_mesg++, ptr++)
	*ptr_mesg = *ptr;
    *ptr_mesg = '\0';
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -