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

📄 send.cpp.old

📁 一个国外学生做的基于Web浏览器的email程序
💻 OLD
字号:
#include "webemail.h"#include "fullpipe.h"//not used, I don't thinkvoid sendmulti(){	//sendmulti();}//not used, I don't thinkvoid sendtext(){	char data[500];	cout << "Content-type: plain/text" << endl;	cout << "Pragma: no-cache" << endl << endl;	while (fgets(data, 499, stdin)!=NULL)	{		cout << data;	}	return;}void send() //spawns sendmail to send mail{	passwd *info;	char boundary[300];	char newboundary[302];	char *lengths; 	int length = 0;	char *data;	char *env;	int stage = 0;	char *to = NULL;	char *cc = NULL;	char *subject = NULL;	char *message = NULL;	char *file = NULL;   //file data to send	char *filename = NULL;	char *receipt = NULL;	char *replied = NULL;	int filesize = 0;	info=getpwnam(globaluser);	if (info == NULL)	{		error("send(): Can't get user data");		return;	}	env = getenv("CONTENT_TYPE");	if (env == NULL)	{		error("send(): boundary data not found");		return;	}	if(finddata(env, "boundary=", boundary)==NULL)	{		error("Send(): Can't find boundary");		return;	}	snprintf(newboundary, 302, "--%s", boundary);	lengths = getenv("CONTENT_LENGTH");	if (lengths == NULL)	{		error("send(): Length data not found");		return;	}	length = atoi(lengths);			data = new char[length+10];	fread(data, 1, length, stdin);	int offset = 0;	while (offset < length)	{		if (strncmp("name=\"to\"", data+offset, 9) == 0)		{			while (data[offset] != '\n' && offset < strlen(data))			{				offset++;			}			offset+=3;  //takes care of the header separator			to = data+offset;			while (offset < (length-strlen(newboundary)))			{				if (strncmp(data+offset, newboundary, strlen(newboundary)) == 0)				{					data[offset-2] = '\0';					break;				}				offset++;			}		}		if (strncmp("name=\"cc\"", data+offset, 9) == 0)		{			while (data[offset] != '\n' && offset < length)			{				offset++;			}			offset+=3;  //takes care of the header separator			cc = data+offset;			while (offset < (length-strlen(newboundary)))			{				if (strncmp(data+offset, newboundary, strlen(newboundary)) == 0)				{					data[offset-2] = '\0';					break;				}				offset++;			}		}		if (strncmp("name=\"subject\"", data+offset, 14) == 0)		{			while (data[offset] != '\n' && offset < length)			{				offset++;			}			offset+=3;  //takes care of the header separator			subject = data+offset;			while (offset < (length-strlen(newboundary)))			{				if (strncmp(data+offset, newboundary, strlen(newboundary)) == 0)				{					data[offset-2] = '\0';					break;				}				offset++;			}		}		if (strncmp("name=\"message\"", data+offset, 14) == 0)		{			while (data[offset] != '\n' && offset < length)			{				offset++;			}			offset+=3;  //takes care of the header separator			message = data+offset;			while (offset < (length-strlen(newboundary)))			{				if (strncmp(data+offset, newboundary, strlen(newboundary)) == 0)				{					data[offset-2] = '\0';					break;				}				offset++;			}		}		if (strncmp("filename=\"", data+offset, 10)==0)		{			filename=data+offset+10;			offset+=10;			while (data[offset] != '"' && offset < length)			{				offset++;			}			data[offset] = '\0';			while (!(data[offset] == 10 && data[offset-1] == 13 &&			data[offset-2] == 10 && data[offset-3] == 13) &&			offset < length)			{				offset++;			}			offset++;			file = data+offset;			while (offset < (length-strlen(newboundary)))			{				if (strncmp(data+offset, newboundary, strlen(newboundary)) == 0)				{					data[offset-2] = '\0';					filesize = (data+offset-2)-file;					break;				}				offset++;			}		}		if (strncmp("name=\"receipt\"", data+offset, 14) == 0)		{			while (data[offset] != '\n' && offset < length)			{				offset++;			}			offset+=3;  //takes care of the header separator			receipt = data+offset;			while (offset < (length-strlen(newboundary)))			{				if (strncmp(data+offset, newboundary, strlen(newboundary)) == 0)				{					data[offset-2] = '\0';  //sets null					break;				}				offset++;			}		}		if (strncmp("name=\"replied\"", data+offset, 14) == 0)		{			while (data[offset] != '\n' && offset < length)			{				offset++;			}			offset+=3;  //takes care of the header separator			replied = data+offset;			while (offset < (length-strlen(newboundary)))			{				if (strncmp(data+offset, newboundary, strlen(newboundary)) == 0)				{					data[offset-2] = '\0';  //sets null					break;				}				offset++;			}		}		offset++;	}	//write data to sendmail	FILE * out;	out = popen(SENDMAIL, "w");	if (out == NULL)	{		error("Send(): Error opening or piping to sendmail");		return;	}	FILE * sentfile;	char sentpath[400];	snprintf(sentpath, 400, "%s/.webmail/sent", info->pw_dir);	sentfile = fopen(sentpath, "a");	if (sentfile == NULL)	{		pclose(out);		error("Send(): Error opening sent file");		return;	}	if (bothlock(sentfile, 'w')!=0)	{		pclose(out);		fclose(sentfile);		error("Send(): Error locking sentfile");	}	fprintf(sentfile, "\nFrom \n");	//sending to	if (to[0] == '\0')	{		error("Send(): \"To\" field is not filled in.");		return;	}	removereturn(to);	fprintf(out, "To: %s\n", to);	fprintf(sentfile, "To: %s\n", to);	//sending Return-Path	FILE *returnfile;	char returnpath[400];	char returnaddr[400];	snprintf(returnpath, 400, "%s/.webmail/return",info->pw_dir);	returnfile = fopen(returnpath, "r");	if (returnfile != NULL && USERRETURN)	{		for (int i = 0; i < 399; i++)		{			returnaddr[i] = fgetc(returnfile);			if (returnaddr[i] == EOF)			{				returnaddr[i] = '\0';				break;			}		}		fclose(returnfile);		fprintf(out, "Reply-to: <%s>\n", returnaddr);		fprintf(sentfile, "Reply-to: <%s>\n", returnaddr);		fprintf(out, "From: %s\n", returnaddr);		fprintf(sentfile, "From: %s\n", returnaddr);		if (receipt && strncmp(receipt, "on", 2)==0)			fprintf(out, "Return-Receipt-To: %s\n", returnaddr);	}	else	{		if (HOSTMODE == 2) //defined		{			fprintf(out, "Reply-to: <%s@%s>\n", info->pw_name, HOSTNAME);			fprintf(sentfile, "Reply-to: <%s@%s>\n", info->pw_name, HOSTNAME);			fprintf(out, "From: %s@%s\n", info->pw_name, HOSTNAME);			fprintf(sentfile, "From: %s@%s\n", info->pw_name, HOSTNAME);			if (receipt && strncmp(receipt, "on", 2)==0)				fprintf(out, "Return-Receipt-To: %s@%s\n", info->pw_name, HOSTNAME); 		}		else if (HOSTMODE == 1)		{			char *host;			if ((host = getenv("SERVER_NAME")) != NULL) //Get return path from server			{ 				fprintf(out, "Reply-to: <%s@%s>\n", info->pw_name, host); 				fprintf(sentfile, "Reply-to: <%s@%s>\n", info->pw_name, host);				fprintf(out, "From: %s@%s\n", info->pw_name, host);				fprintf(sentfile, "From: %s@%s\n", info->pw_name, host);				if (receipt && strncmp(receipt, "on", 2)==0)					fprintf(out, "Return-Receipt-To: %s@%s\n", info->pw_name, host); 			}			else //use sendmail if httpd fails			{				fprintf(out, "From: %s\n", info->pw_name);				fprintf(sentfile, "From: %s\n", info->pw_name);				if (receipt && strncmp(receipt, "on", 2)==0)			 		fprintf(out, "Return-Receipt-To: %s\n", info->pw_name); 		 	}		}		else //Let sendmail fill in return path		{			fprintf(out, "From: %s\n", info->pw_name);			fprintf(sentfile, "From: %s\n", info->pw_name);			if (receipt && strncmp(receipt, "on", 2)==0)		 		fprintf(out, "Return-Receipt-To: %s\n", info->pw_name); 		}	}	//sending X-Mailer	fprintf(out, "X-Mailer: WEBEMAIL %s\n", VER);	fprintf(sentfile, "X-Mailer: WEBEMAIL %s\n", VER);	//sending cc	if (cc[0] != '\0')	{		removereturn(cc);		fprintf(out, "Cc: %s\n", cc);		fprintf(sentfile, "Cc: %s\n", cc);	}	//sending subject	removereturn(subject);	fprintf(out, "Subject: %s\n", subject);	fprintf(sentfile, "Subject: %s\n", subject);	//sending header break	fprintf(out, "\n");  //header break	fprintf(sentfile, "\n");  //header break	//sending message	fprintf(out, "%s", message); 	fprintf(sentfile, "%s", message); 	if (filesize > 0)	{		char uuencodepath[500];		char buf;		int pipe[2];		snprintf(uuencodepath, 500, "/usr/bin/uuencode");		char *arg[3];		arg[0] = "uuencode";		arg[1] = filename;		arg[2] = '\0';		//make sure any child process don't have data in the buffer		//that might be writen on their termination.		fflush(out);		fflush(sentfile);		fflush(stdout);		fflush(stdin);		if (fullpipe_arg(uuencodepath, arg, pipe))		{			error("send(): Problem with pipe");			return;		}		//fork to prevent blocking		int forkresult;		forkresult = fork();		if (forkresult == (pid_t)-1)		{			error("send(): Problem forking");			return;		}		if (forkresult == (pid_t)0) //child		{			write(pipe[1], file, filesize);			close(pipe[1]);			exit(0);		}		else //parent		{			close(pipe[1]); //prevents block on open write			fputs("\n\n", out);			fputs("\n\n", sentfile);			while (read(pipe[0], &buf, 1))			{				fputc(buf, out);				fputc(buf, sentfile);			}			close(pipe[0]);		}	}	//MARK REPLIED BEFORE LETTING SENDMAIL SEND	//OR SENDMAILS MESSAGE MAY BE OVER WRITTEN	int repliednum = atoi(replied);	if (repliednum > 0)	{		markreplied(repliednum);	}	//displays sendmail return errors 	if (pclose(out) != 0)	{		error("sendmail: bad local \"to\"/\"cc\" address or internal sendmail problem.");		return;	}	fclose(sentfile);	disjavascript("", "Mail sent", "menu");	delete(data);}void sendto() //sets up write to send mail{	char *env;	int number = 0;	FILE *data;	char dataname[400];	passwd *info;	char buf[500];	if ((env = getenv("QUERY_STRING")) == NULL)	{		error("sendto():environment error");		return;	}	number = atoi(env+9);	info = getpwnam(globaluser);	snprintf(dataname, 400, "%s/.webmail/book", info->pw_dir);	data = fopen(dataname, "r");	if (data == NULL)	{		error("sendto():can't open book for read");		return;	}	if (bothlock(data, 'r')!=0)	{		fclose(data);		error("sendto():Can't lock book for read");		return;	}	for (int i = 0; i < (number-1)*7+2; i++)	{		if (fgets(buf, 499, data) == NULL)		{			error("sendto():bad input");			return;		}	}	bothunlock(data);	fclose(data);	wewrite(2, buf, 0);	return;}

⌨️ 快捷键说明

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