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

📄 sent.cpp

📁 一个国外学生做的基于Web浏览器的email程序
💻 CPP
字号:
#include "sent.h"#include "webemail.h"#include "attachments.h"#include "delete.h"#include <vector>void dissent(){	passwd *info;	FILE *mailstore;	char filename[400];	char data[500];	int emailcount = 0;	char from[101];	char subject[101];	from[0] = '\0';	subject[0] = '\0';	int end = 0;	int onheader = 0;	int lowercount = 0;  //used so that the count is correct with hiden message.	info=getpwnam(globaluser);	if (info == NULL)	{		error("dissent(): Can't read user data");		return;	}		cout << "content-type: text/html" << endl;	cout << "Pragma: No-Cache" << endl << endl;	cout << "<html>" << endl;	cout << "<head>" << endl;	cout << "<title>Web E-Mail</title>" << endl;	cout << "<meta http-equiv=\"expires\" content=\"0\">" << endl;	cout << "</head>" << endl;	cout << "<body text=\"#000000\" bgcolor=\"#FFFFFF\" ";	cout << "link=\"#0000EF\" vlink=\"#0000EF\" alink=\"#FF0000\" ";	cout << "background=/email/bg1sent.gif>";	cout << "<font size=\"+2\">Sent Mail Box</font><br>";	snprintf(filename, 400, "%s/.webmail/sent", info->pw_dir);	mailstore = fopen(filename, "r");	if (mailstore == NULL)	{		//don't use error() here		cout << "No sent mail.";		return;	}	if (bothlock(mailstore, 'r')!=0)	{		cout << "Locking Error!!";		return;	}	cout << "<form action=\"" << EMAILPATH << "?sentmultidel\" "<<"method=\"post\">";    	while (end == 0)  	{		if (fgets(data, 499, mailstore) == NULL)		{			end = 1;		}		if (data[0] == '\n' || data[0] == '\r')		{			onheader=0;		}		if (strncasecmp(data, "From: ", 6)==0 && onheader)		{			strncpy(from, data, 99);			from[100]='\0';					}		if (strncasecmp(data, "Subject: ", 9)==0 && onheader)		{			strncpy(subject, data, 99);			subject[100]='\0';		}		if (strncmp(data, "From ", 5)==0 || end == 1)		{			onheader = 1;			if (from[0] != '\0')			{				emailcount++;				if (emailcount == 1 && 				strncasecmp(from, "From: Mail System Internal Data", 31) == 0)				{					lowercount = 1;				}				else				{					cout << "<img SRC=\"/email/old.jpeg\" ALT=\"(" << WEM_Old << ")\"";					cout << "BORDER=0 height=14 width=20>" << endl;					cout << "&nbsp;<input TYPE=\"checkbox\" NAME=\"m" << emailcount << "\">&nbsp;" << endl;					cout << "<a href=\"" << EMAILPATH << "?showsent=" << emailcount << "\" TARGET=_top>" << endl;	 				escapeprint(from);					cout << "</a>" << endl;					if (subject != '\0')					{						cout << "<br>&nbsp;&nbsp;";						escapeprint(subject);						cout << endl;					}					cout << "&nbsp;&nbsp;<a href=\"" << EMAILPATH << "?deletesent=" << emailcount << "\" TARGET=_top>";					cout << WEM_Delete << "</a><br><br>" << endl;				}				from[0] = '\0';				subject[0] = '\0';			}		}	}	cout << "<Input TYPE=submit Value=\"";	cout << WEM_Delete_checked_mail << "\"></form>" << endl;	cout << "<font size=\"+2\">Sent Mail Box</font><br>";	cout << WEM_E_Mail_count << ": ";	if (lowercount == 1)		cout << (emailcount - 1);	else		cout << emailcount;	cout << "<br>";	cout << "<a name=bottom></a>" << endl;	cout << "</body>" << endl;	cout << "</html>" << endl;	bothunlock(mailstore);	fclose(mailstore);	return;}void showsent(){	char *env;	int number; 	//email to read	passwd *info;	FILE *mailstore;	char filename[400];	char data[500];	int count = 0;	int offofheader = 0; //mail header	int extenddata = 0; //multi line headers	int domarkold = 1;	int ismime = 0; //looks for mime header, only turns on for 1.0	int onmimeheader = 0;	char answer[500];	char boundary[300];	boundary[0] = '-'; //sets up boundary	boundary[1] = '-'; // "   "     "	//attachment stuff	int attachcount = 0;	int dontdisplay = 0;	char *attachname[100];	for (int loop = 0; loop < 100; loop++)	{		attachname[loop] = NULL;	}	if ((env = getenv("QUERY_STRING"))==NULL)	{		disjavascript("?menusent", "Sent mail deleted", "menu");		return;	}	number = atoi(env+9);		info=getpwnam(globaluser);	if (info == NULL)	{		error("showsent():Can't get user info");		return;	}	snprintf(filename, 400, "%s/.webmail/sent", info->pw_dir);	mailstore = fopen(filename, "r");	if (mailstore == NULL)	{		error("showsent():ERROR while tring to read mail file");		return;	}		if (bothlock(mailstore, 'r')!=0)	{		fclose(mailstore);		error("showsent():Error while locking mail file.");		return;	}	cout << "content-type: text/HTML" << endl;	cout << "Pragma: No-cache" << endl << endl;	cout << "<html><head>" << endl;	cout << "<title>Web E-Mail</title>" << endl;	cout << "<META HTTP-EQUIV=\"expires\" content=\"0\">" << endl;	cout << "</head>" << endl;	cout << "<body text=\"#000000\" bgcolor=\"#FFFFFF\" ";	cout << "link=\"#0000EF\" vlink=\"#0000EF\" alink=\"#FF0000\" background=/email/bg1sent.gif>" << endl;		header("Sent Mail");	cout << "<table CELLSPACING=0 CELLPADDING=0 NOSAVE >" << endl;	cout << "<tr NOSAVE><td NOSAVE>" << endl;	cout << "<table CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\" NOSAVE >" << endl;	cout << "<tr NOSAVE><td ALIGN=LEFT VALIGN=BOTTOM NOSAVE>" << endl;	cout << "</td>" << endl;	cout << "<td ALIGN=RIGHT VALIGN=BOTTOM NOSAVE>" << endl;	cout << "<a href=\"" << EMAILPATH << "?deletesent=" << number << "\">" << WEM_Delete << "</a>";	cout << "</td></tr></table></td></tr>" << endl;	cout << "<tr NOSAVE><td NOSAVE>" << endl;	cout << "<form><textarea wrap=virtual cols=80 rows=20>" << endl;	//display message	while (fgets(data, 499, mailstore) != NULL)	{		if (strncmp(data, "From ", 5)==0)		{			count++;		}		if (count == number)		{			//tries to prevent js attacks			distroytag(data, "</textarea");			if (data[0] == '\n')				offofheader = 1; 			if (offofheader == 0) //in header			{				if (strncasecmp(data,"From: ", 6)==0 ||				strncasecmp(data, "To: ", 4) == 0 ||				strncasecmp(data, "Date: ", 6)==0 ||				strncasecmp(data, "cc: ", 4)==0 ||				strncasecmp(data, "Reply-To: ", 10)==0)				{					extenddata = 1;					cout << data;				}				else if (strncasecmp(data, "Subject: ",9)==0)				{					extenddata = 1;					cout << data;				}				else if (strncasecmp(data, "MIME-Version: 1.0",17)==0)				{					cout << data;					ismime=1;				}				//else if (strncasecmp(data, " boundary=", 10) == 0 ||				//strncasecmp(data, "\tboundary=", 10) ==0)				else if (finddata(data, "boundary=", answer) != NULL)				{					int o = 0;					if (answer[0] == '"')						o++; //checks for" 					strncpy(boundary+2, answer+o, 300);					for (o = 0; o<299; o++)					{ 								if (boundary[o] == '\n' ||	 					boundary[o] == '"' || 							boundary[o] == '\r' || 							boundary[o] == '\0') 							{ 										boundary[o] = '\0';	 						break; 				 			} 									}  				} 				else if (extenddata == 1 && 				strncasecmp(data, "        ",8)==0)				{					cout << data;				}				else //turn off extenddata				{					extenddata = 0;				}			}			else //out of header			{				//turns on message display.				if (USEATTACHMENTS && ismime)				{					if (strncmp(data, boundary,strlen(boundary))==0) 					{						onmimeheader = 1;						dontdisplay = 0;						//default on, in case there is no						// content-type						if (attachcount < 100)						{							attachcount++;							attachname[attachcount] = new char[500]; 										//strncpy(attachname[attachcount], data, 499); 								snprintf(attachname[attachcount],499, "          mime attachment %d", attachcount);							attachname[attachcount][499] = '\0';  						}		 			}					if (onmimeheader && (					finddata(data, "filename=", answer) != NULL))					{						//is this safe???						//has memory been defined already???						strncpy(attachname[attachcount], "          ", 499);						strncpy(attachname[attachcount]+10, answer, 489);					}					if (onmimeheader && (					strncasecmp(data, "Content-Type: text/plain",24)==0 ||					strncasecmp(data, "Content-Type: message/delivery-status",37)==0					))					{						//turn on display, or 						//really leave on						dontdisplay = 0; 					}					else if (onmimeheader &&					strncasecmp(data, "Content-Type: ", 14) == 0)					{						//unknown type, turn off display						dontdisplay = 1;					}					else {}				}				else if (USEATTACHMENTS && !ismime)				{				  	if (strncasecmp(data, "end", 3) ==0)						dontdisplay = 0;				}				else {}				//displays message				if (USEATTACHMENTS && 				dontdisplay == 0 && onmimeheader == 0)				{	cout << data;} //ok to display				else if (USEATTACHMENTS)				{} //not ok to display				else				{	cout << data;} //not in attachment mode ok to dis				//turns off message display				if (USEATTACHMENTS && ismime)				{					if (onmimeheader && strncmp(data, "\n", 1) == 0)					{						onmimeheader = 0;					}				}				else if (USEATTACHMENTS && !ismime)				{					if (strncmp(data, "begin ", 6)==0)					{						dontdisplay = 1;						if (attachcount < 100)						{							attachcount++;							cout << "Attached file removed from display." << endl;							attachname[attachcount] = new char[500];							strncpy(attachname[attachcount], data, 499);							attachname[attachcount][499] = '\0'; 						}				    	}				}				else {}			} // off header		}//attach count == num	} //while	cout << "</textarea></form>" << endl;	cout << "</td></tr></table>";	if (USEATTACHMENTS)	{	 if (attachcount > 0)	 {		cout << "<font size=\"+2\"><u>"; 		cout << WEM_Attachments << "</u></font><br>";		if (ismime)		{			attachcount--; //lower count because last break gets counted		}				for (int loop = 1; loop <= attachcount; loop++)		{			cout << "<a href=\"" << "./attachments/";			cout << cleanfilename(attachname[loop]+10);			if (ismime)				cout << "?mshowsentattach=" << number << "x";			else				cout << "?showsentattach=" << number << "x";			cout << loop << "\">";			cout << attachname[loop]+10;			cout << "</a><br>";			delete(attachname[loop]);		}		cout << "<br>";	 }	}	tail();		bothunlock(mailstore);	fclose(mailstore);	cout << "</body></HTML>" << endl;	return;}void deletesent(){	char *env;	int mailnum;	char mailbox[400];	passwd *info;		if ((env = getenv("QUERY_STRING"))==NULL)	{		disjavascript("?menusent", "delete error", "menu");		return;	}	mailnum = atoi(env+11);	info=getpwnam(globaluser);	snprintf(mailbox, 400, "%s/.webmail/sent", info->pw_dir);	deletemail(mailbox, mailnum);  	disjavascript("?menusent", "Sent mail deleted", "menu");}void multidelsent(){	char data;	char number[10];	vector <int> todel;	int i = 0;	int mark = 0;	int k =0;	while (1)	{		data = fgetc(stdin);		if (data == EOF)			break;		if (data == '=')		{			mark = 0;			number[k] = '\0';			k = 0;			//cout << atoi(number) << endl;			todel.push_back(atoi(number));		}				if (mark == 1)		{			number[k] = data;			k++;		}		if (data == 'm')		{			mark=1;			k=0;		}	}	multideletemailsent(todel);	return;}void multideletemailsent(vector<int>& todel){	passwd *info;	FILE *data;	FILE *tmp;	char dataname[400];	char tmpname[400];	char buf[500];	int number = 0;	int emailcount = 0;	int delme = 0;	info=getpwnam(globaluser);	snprintf(dataname, 400, "%s/.webmail/sent", info->pw_dir);	snprintf(tmpname, 400, "%s/.webmail/tmp", info->pw_dir);		data = fopen(dataname, "r");	if (data == NULL)	{		error("multideletemailsent():Error opening data for read");		return;	}	if (bothlock(data, 'r')!=0)	{		fclose(data);		error("multideletemailsent():Error locking data for read");		return;	}	tmp = fopen(tmpname, "w");	if (tmp == NULL)	{		error("multideletemailsent():error opening temp for write");		fclose(data);		return;	}	if (bothlock(tmp, 'w')!=0)	{		fclose(data);		fclose(tmp);		error("multideletemailsent():Error locking tmp for write");		return;	}	while (fgets(buf, 499, data) != NULL)	{		fputs(buf, tmp);	}	bothunlock(data);	fclose(data);	bothunlock(tmp);	fclose(tmp);	//rename(dataname, tmpname);	sync();	data = fopen(dataname, "w");	if (data == NULL)	{		error("multideletemailsent():Error opening data for write");		return;	}	if (bothlock(data, 'w')!=0)	{		fclose(data);		error("multideletemailsent():Error locking data for write");		return;	}	tmp = fopen(tmpname, "r");	if (tmp == NULL)	{		error("multideletemailsent():error opening temp for read");		fclose(data);		return;	}	if (bothlock(tmp, 'r')!=0)	{		fclose(data);		fclose(tmp);		error("multideletemailsent():Error locking temp for read");		return;	}		while (fgets(buf, 499, tmp) != NULL)	{		if (strncmp(buf, "From ", 5)==0)		{			emailcount++;			delme = 0;			for (int p = 0; p<todel.size();p++)			{				if (emailcount == todel[p])				{					delme = 1;				}			}		}		if (delme == 0)		{			fputs(buf, data);		}	}	bothunlock(tmp);	fclose(tmp);	bothunlock(data);	fclose(data);	remove(tmpname);	sync();	disjavascript("?dissent#bottom", "Mail deleted", "mail list");	return;}void showsentattach(){	char *env;	char *env2;	int loop = 15;	int attachnum;	int mailnum;	char mailbox[400];	passwd *info;		if ((env = getenv("QUERY_STRING"))==NULL)	{		disjavascript("?menusent", "Attach display error", "menu");		return;	}	while (env[loop] != '\0')	{		if (env[loop] == 'x')		{			env[loop] = '\0';			env2=env+loop+1;					}		loop++;	}	attachnum = atoi(env2);	mailnum = atoi(env+15);	info=getpwnam(globaluser);	snprintf(mailbox, 400, "%s/.webmail/sent", info->pw_dir);	showattachment(mailbox, mailnum, attachnum);  	}void mshowsentattach(){	char *env;	char *env2;	int loop = 16;	int attachnum;	int mailnum;	char mailbox[400];	passwd *info;		if ((env = getenv("QUERY_STRING"))==NULL)	{		disjavascript("?menusaved", "Attach display error", "menu");		return;	}	while (env[loop] != '\0')	{		if (env[loop] == 'x')		{			env[loop] = '\0';			env2=env+loop+1;					}		loop++;	}	attachnum = atoi(env2);	mailnum = atoi(env+16);	info=getpwnam(globaluser);	snprintf(mailbox, 400, "%s/.webmail/sent", info->pw_dir);	mshowattachment(mailbox, mailnum, attachnum);}

⌨️ 快捷键说明

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