Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3368 leency 1
//Leency & SoUrcerer, LGPL
2
 
4139 hidnplayr 3
void ParseMail()
3368 leency 4
{
5
	dword line_off, new_buf;
6
	char tline[256];
7
 
4166 leency 8
	if ( mailend-mailstart > 9) if (strncmp(mailend-5,"\n.\n",5)==0) // note that c-- assembles "\n.\n" to 0x0d, 0x0a, 0x2e, 0x0d, 0x0a
3368 leency 9
	{
4166 leency 10
		mailend -= 5;
11
		if (strstr(mailstart, "+OK")!=mailstart)
3368 leency 12
		{
13
			aim = GET_ANSWER_RETR;
4166 leency 14
			mailend = mailstart;
3368 leency 15
			debug("GET_ANSWER_RETR != +OK, retry GET_ANSWER_RETR");
16
			return;
17
		}
18
		aim=NULL;
4166 leency 19
		DSBYTE[mailend] = '\0';
20
		mailsize = mailend - mailstart;
3368 leency 21
 
4166 leency 22
		if (strstri(mailstart, "quoted-printable")!=0)
3368 leency 23
		{
4166 leency 24
			new_buf = malloc(mailend-mailstart);
25
			qp_decode stdcall (mailstart, new_buf, mailend-mailstart);
3368 leency 26
			if (EAX==-1) debug("Too small buffer to convert QUOTED-PRINTABLE");
27
			else
28
			{
4166 leency 29
				free(mailstart);
30
				mailstart = new_buf;
31
				mailsize = strlen(mailstart);
32
				mailend = mailsize + mailstart;
3368 leency 33
			}
3413 leency 34
		}
3368 leency 35
		GetHeader(#from, "\nFrom:");
36
		GetHeader(#to,   "\nTo:");
37
		GetHeader(#date, "\nDate:");
38
		GetHeader(#subj, "\nSubject:");
4166 leency 39
		mdata = strstr(mailstart, "\n\r") + 3;		// 0x0d 0x0a, 0x0a
40
		ConvertToDOS(mdata, mailstart);
3368 leency 41
		FromHTMLtoTXT();
42
		letter_view.first = letter_view.count = 0;
43
 
44
		line_off = mdata;
45
		while (line_off>1)
46
		{
47
			line_off = GetNextLine(line_off);
48
			letter_view.count++;
49
		}
3413 leency 50
		atr.SetAtrFromCurr(mail_list.current+1);
3368 leency 51
		DrawMailBox();
52
	}
53
}
54
 
55
void ConvertToDOS(dword inbuf, searchin)
56
{
57
	dword dos_buf=0;
58
	if (use_iconv==1)
59
	{
60
		cur_charset = CH_CP866;
61
		if (strstri(searchin, "windows-1251")!=0) || (strstri(searchin, "windows1251")!=0)
62
			{ dos_buf = ChangeCharset("CP1251", "CP866", inbuf);    cur_charset = CH_CP1251;}
63
		else if (strstri(searchin, "koi8-")!=0)
64
			{ dos_buf = ChangeCharset("KOI8-RU", "CP866", inbuf);   cur_charset = CH_KOI8;}
65
		else if (strstri(searchin, "utf-8")!=0) || (strstri(searchin, "utf8")!=0)
66
			{ dos_buf = ChangeCharset("UTF-8", "CP866", inbuf);     cur_charset = CH_UTF8;}
67
		else if (strstri(searchin, "iso8859-5")!=0) || (strstri(searchin, "iso-8859-5")!=0)
68
			{ dos_buf = ChangeCharset("ISO8859-5", "CP866", inbuf); cur_charset = CH_ISO8859_5;}
69
		else if (strstri(searchin, "windows-1252")!=0) || (strstri(searchin, "windows1252")!=0)
70
			{ dos_buf = ChangeCharset("CP1252", "CP866", inbuf);    cur_charset = CH_CP1252;}
71
	}
72
	else
73
	{
74
		if (strstri(searchin, "windows-1251")!=0) wintodos( inbuf); else
75
		if (strstri(searchin, "koi8-")!=0)        koitodos( inbuf); else
76
		if (strstri(searchin, "utf-8")!=0)        utf8rutodos( inbuf);
77
	}
78
}
79
 
80
 
81
void FromHTMLtoTXT()
82
{
83
	dword cur_chr, txt_buf_srt, txt_buf_end, is_tag=0;
84
	int i;
4166 leency 85
	if (strstri(mdata, "")==0) && (strstri(mailstart, "text/html")==0) {debug("no html tags found"); return;}
3368 leency 86
	debug ("converting: html -> txt");
87
	cur_chr = mdata;
4166 leency 88
	txt_buf_srt = malloc(mailend - mailstart);
3368 leency 89
	txt_buf_end = txt_buf_srt;
90
 
4166 leency 91
	while (cur_chr < mailend)
3368 leency 92
	{
93
		if (DSBYTE[cur_chr]=='<') is_tag = 1;
94
		if (!is_tag)
95
		{
96
		 	DSBYTE[txt_buf_end] = DSBYTE[cur_chr];
97
		 	txt_buf_end++;
98
		 	_END:
99
		}
100
		if (DSBYTE[cur_chr]=='>') is_tag = NULL;
101
		cur_chr++;
102
	}
103
	DSBYTE[txt_buf_end] = '\0';
104
	strcpy(mdata, txt_buf_srt);
4166 leency 105
	mailend = strlen(mailstart) + mailstart;
3368 leency 106
	free(txt_buf_srt);
107
}
108
 
109
 
110
dword GetNextLine(dword start_offset)
111
{
112
	dword off_n = strstr(start_offset, "\n") + 1,  //разрыв строки
113
	      off_w = letter_view.w / 6 - 2 + start_offset, //max длинна скроки
114
	      off_m;
115
	off_m = off_w;
116
	if (off_n < off_w) return off_n;
117
	while (off_m > start_offset) //перенос по словам
118
	{
119
		if (DSBYTE[off_m]==' ') || (DSBYTE[off_m]=='\9') || (DSBYTE[off_m]=='-') return off_m;
120
		off_m--;
121
	}
122
	return off_w;
123
}
124
 
125
dword CopyBetweenOffsets(dword start, end) //do not forget to free(line) after use
126
{
127
	dword line, new_line;
128
	if (end <= start) return 0;
129
	line = new_line = malloc(end - start + 3);
130
	while (end > start)
131
	{
132
		DSBYTE[new_line] = DSBYTE[start];
133
		start++;
134
		new_line++;
135
	}
136
	DSBYTE[new_line] = '\0';
137
	return line;
138
}
139
 
140
void GetHeader(dword workstr, searchstr)
141
{
142
	char tmpbuf[512];
143
	dword Qoff;
144
 
4166 leency 145
	strcpyb(mailstart, workstr, searchstr, "\n");
3368 leency 146
	if (strstri(workstr, "?Q?"))
147
	{
148
		qp_decode stdcall (workstr, #tmpbuf, strlen(workstr));
149
		ConvertToDOS(#tmpbuf, workstr);
150
		strcpy(workstr, #tmpbuf);
151
		Qoff = strstri(workstr, "?Q?");
152
		strcpy(workstr, Qoff);
153
	}
154
	if (strstr(workstr, "?B?"))
155
	{
156
		base64_decode stdcall (strstri(workstr, "?B?"), #tmpbuf, strlen(workstr));
157
		ConvertToDOS(#tmpbuf, workstr);
158
		strcpy(workstr, #tmpbuf);
159
	}
160
	if (strlen(workstr)+10*6-Form.cwidth>0) { workstr=Form.cwidth/6+workstr-12; DSBYTE[workstr]='\0';}
161
}