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

📄 pop3io.c

📁 LINUX下的收发E-MAIL的程序
💻 C
字号:
#include 	"pop3io.h"
#include	<assert.h>


char *pop3_errlist[] = { "",
    ": Hostname failed to resolve",
    ": Socket failed to open",
    ": No route to host?",
    ": Server failed to acknowledge",
    ": USER send()",
    ": USER recv()",
    ": PASS send()",
    ": PASS recv()",
    ": LIST send()",
    ": LIST recv()",
    ": STAT send()",
    ": STAT recv()",
    ": UIDL send()",
    ": UIDL recv()",
    ": RETR send()",
    ": RETR recv()",
    ": RSET send()",
    ": RSET recv()",
    ": QUIT send()",
    ": QUIT recv()",
    ": DELE send()",
    ": DELE recv()"
};

extern int errno;
pop3errnos pop3errno;
extern struct RmailList popmail;
extern unsigned int emlTimeout;


char *pop3error(void)
{
    return pop3_errlist[pop3errno];
}


int eml_pop3_user(int sock, char *login)
{
    char buffer[256];
    char command[20];

    snprintf(command, 20, "USER %s\r\n", login);

    if (send(sock, command, strlen(command), 0) < 0) {
	pop3errno = P_ERR_USER_SEND;
	return ERROR;
    }

    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	pop3errno = P_ERR_USER_RECV;
	return ERROR;
    }
    return SUCCESS;
}


int eml_pop3_pass(int sock, char *passwd)
{
    char buffer[256];
    char command[20];

    snprintf(command, 20, "PASS %s\r\n", passwd);
    if (send(sock, command, strlen(command), 0) < 0) {
	pop3errno = P_ERR_PASS_SEND;
	return ERROR;
    }

    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	pop3errno = P_ERR_USER_RECV;
	return ERROR;
    }

    return SUCCESS;
}


int eml_pop3_stat(int sock)
{
    char buffer[256];
    char command[7];
    int num, size;

    strncpy(command, "STAT\r\n", 7);
    if (send(sock, command, strlen(command), 0) < 0) {
	pop3errno = P_ERR_STAT_SEND;
	return ERROR;
    }
    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	pop3errno = P_ERR_STAT_RECV;
	return ERROR;
    }
    sscanf(buffer, "+OK %d %d messages\r\n", &num, &size);
    return num;
}


int eml_pop3_list(int sock)
{
    char buffer[256];
    char command[7];
    int num = 0;

    strncpy(command, "LIST\r\n", 7);
    if (send(sock, command, strlen(command), 0) < 0) {
	pop3errno = P_ERR_LIST_SEND;
	return ERROR;
    }

    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	pop3errno = P_ERR_LIST_RECV;
	return ERROR;
    }
    while (recvline(sock, buffer, 255) > 0) {
	num++;
    }

    num--;

    return num;
}

/*
int eml_pop3_uidl(int sock, unsigned int id, char *uid)
{
	char buffer[256];
	char command[20];
	int id2;
	int n;

	snprintf(command, 20, "UIDL %d\r\n", id);
	if (send (sock, command, strlen(command), 0) < 0)
	{
		pop3errno = P_ERR_UIDL_SEND;
		return ERROR;
	}

	if ( (n = recvline(sock, buffer, 255) < 0) 
		|| (find_string(buffer, "+OK") == -1) )
	{
		if (n < 0)
			fprintf(stderr, "n < 0\n");
		if (find_string(buffer, "+OK") == -1)
			fprintf(stderr, "couldn't find \"+OK\" -> %s\n", buffer);
		pop3errno = P_ERR_UIDL_RECV;
		return ERROR;
   }
	sscanf(buffer, "+OK %d %s", &id2, uid);
	return SUCCESS;
}*/

int eml_pop3_retr(int sock, unsigned int id)
{
    char buffer[256];
    char command[10];
    char *mailall = NULL;
    int len = 0, total = 0;
    int n;
    char *mailtotal = NULL;
    Rmail *pmail = NULL;

    Rmail *tmp = popmail.mail;
    for (n = 1; n < popmail.total; n++) {
	tmp = tmp->next;
    }
    pmail = (Rmail *) malloc(sizeof(Rmail));
    assert(pmail);
    snprintf(command, 10, "RETR %d\r\n", id);
    if (send(sock, command, strlen(command), 0) < 0) {
	pop3errno = P_ERR_RETR_SEND;
	return -1;
    }
    if ((n = recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	pop3errno = P_ERR_RETR_RECV;
	return -1;
    }
    n = recvline(sock, buffer, 255);
    while (!(n <= 0) && strncmp(buffer, ".\r\n", 3)) {
	strip(buffer, '\r');
	len = strlen(buffer);
	if (len > 0) {
	    total += len;
	    if ((mailall = (char *) realloc(mailall, total + 1)) == NULL)
		printf("Fail to realloc memory\n");
	    strncpy(mailall + total - len, buffer, len);
	    mailall[total] = 0;
	    memset(buffer, 0, sizeof(buffer));
	}
	n = recvline(sock, buffer, 255);
    }

    printf("%s", mailall);

    pmail->sender = eml_mail_get_from(mailall);

    pmail->subject = eml_mail_get_subject(mailall);

    pmail->receiver = eml_mail_get_to(mailall);

    pmail->date = eml_mail_get_date(mailall);

    pmail->messageid = eml_mail_get_messageid(mailall);

    printf("the pmail->messageid: %s\n", pmail->messageid);

    n = ifreceive(pmail->messageid);

    printf("n = %d\n", n);

    if (n) {
	printf("Have received this mail.\n");
	free(mailall);
	free(pmail);
	return 0;
    }

    mailtotal = eml_mail_get_EncodeMethod(mailall);

    n = eml_mime_detect(mailall);
    if (n > 0) {
	eml_mime_parse(mailall, pmail);
	free(mailall);
    } else {
	trim_header(mailall, 2);
	n = strlen(mailall);
	pmail->attach_file = NULL;
	if (mailtotal == NULL) {
	    pmail->content = mailall;
	    goto out;
	}
	if (strncmp(mailtotal, "quoted-printable", 16) == 0) {
	    pmail->content = eml_mime_decode_quoted_printable(mailall, &n);
	    free(mailall);
	} else if (strncmp(mailtotal, "base64", 6) == 0) {
	    pmail->content = eml_mime_decode_base64(mailall, &n);
	    free(mailall);
	} else {
	    pmail->content = mailall;
	}
    }

  out:pmail->next = NULL;
    if (popmail.total == 0)
	popmail.mail = pmail;
    else
	tmp->next = pmail;
    popmail.total++;
    return 0;
}


int eml_pop3_dele(int sock, unsigned int id)
{
    char buffer[256];
    char command[10];
    snprintf(command, 10, "DELE %d\r\n", id);

    if (send(sock, command, strlen(command), 0) < 0) {
	pop3errno = P_ERR_DELE_SEND;
	return ERROR;
    }
    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	pop3errno = P_ERR_DELE_RECV;
	return ERROR;
    }
    return SUCCESS;
}


int eml_pop3_rset(int sock)
{
    char buffer[256];
    char command[7];

    strncpy(command, "RSET\r\n", 7);

    if (send(sock, command, strlen(command), 0) < 0) {
	pop3errno = P_ERR_RSET_SEND;
	return ERROR;
    }
    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	pop3errno = P_ERR_RSET_RECV;
	return ERROR;
    }

    return SUCCESS;
}


int eml_pop3_quit(int sock)
{
    // lets tell the server we want to quit now 
    char buffer[256];
    char command[8];

    strncpy(command, "QUIT\r\n", 8);
    if (send(sock, command, strlen(command), 0) < 0) {
	pop3errno = P_ERR_QUIT_SEND;
	return ERROR;
    }
    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	// mr server didn't say bye-bye :( 
	pop3errno = P_ERR_QUIT_RECV;
	return ERROR;
    }
    return SUCCESS;
}


int eml_pop3_login(Server * server, char *login, char *passwd)
{
    char buffer[256];
    int sock;

    pop3errno = P_NO_ERROR;
    if (!Resolve(server)) {
	pop3errno = P_ERR_RESOLVE;
	return -1;
    }
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0) {
	pop3errno = P_ERR_SOCKET;
	return -1;
    }
    server->sin.sin_family = AF_INET;
    server->sin.sin_port = htons(server->port);

    if (connect_timeo
	(sock, (struct sockaddr *) &server->sin, sizeof(server->sin),
	 emlTimeout) < 0) {
	pop3errno = P_ERR_CONNECT;
	close(sock);
	return -1;
    }
    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, "+OK") == -1)) {
	pop3errno = P_ERR_SERV_ACK;
	close(sock);
	return -1;
    }
    if (!eml_pop3_user(sock, login)) {
	close(sock);
	return -1;
    }
    if (!eml_pop3_pass(sock, passwd)) {
	close(sock);
	return -1;
    }
    return sock;
}


int eml_pop3_logoff(int sock)
{
    close(sock);
    return 0;
}


void prepare_popmail(struct RmailList *mail)
{
    mail->total = 0;
}


void free_popmail(struct RmailList *mail)
{
    char i;
    char j;
    Rmail *p = NULL;
    if (mail->total == 0)
	return;

    for (i = mail->total - 1; i >= 0; i--) {
#ifdef	__DEBUG
	printf(" free pop mail i =%d, .....................\n", i);
#endif
	p = mail->mail;
	for (j = 0; j < i; j++)
	    p = p->next;
	if (p->sender != NULL)
	    free(p->sender);

	if (p->receiver != NULL)
	    free(p->receiver);
	if (p->date != NULL)
	    free(p->date);
	if (p->subject != NULL)
	    free(p->subject);
	if (p->content != NULL)
	    free(p->content);
	if (p->attach_file != NULL)
	    free(p->attach_file);
	if (p->next != NULL)
	    free(p->next);
    }
    return;
}


int eml_pop3_mails(Server * server, char *login, char *passwd, int del_yn)
{
    int sock;
    int num;
    unsigned int i;

    pop3errno = P_NO_ERROR;
    sock = eml_pop3_login(server, login, passwd);
    if (sock < 0) {
	return ERROR;
    }
    if ((num = eml_pop3_stat(sock)) < 0) {
	close(sock);
	return ERROR;
    }
    printf("there are %d emails on server!\n", num);

    for (i = 1; i <= num; i++) {
	if (eml_pop3_retr(sock, i) < 0) {
	    close(sock);
	    return ERROR;
	}
	if (del_yn == 1) {
	    if (!eml_pop3_dele(sock, i)) {
		close(sock);
		return ERROR;
	    }
	}
    }
    if (!eml_pop3_quit(sock)) {
	close(sock);
	return ERROR;
    }
    close(sock);
//      printf("Receive %d mails successfully.\n",num);
    return SUCCESS;
}

⌨️ 快捷键说明

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