Subversion Repositories Kolibri OS

Rev

Rev 4186 | Go to most recent revision | Details | Compare with Previous | 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
 
4417 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
	{
4417 leency 10
		aim = STOP;
4166 leency 11
		mailend -= 5;
4186 leency 12
		DSBYTE[mailend] = '\0';
4166 leency 13
		if (strstr(mailstart, "+OK")!=mailstart)
3368 leency 14
		{
15
			aim = GET_ANSWER_RETR;
4166 leency 16
			mailend = mailstart;
3368 leency 17
			debug("GET_ANSWER_RETR != +OK, retry GET_ANSWER_RETR");
18
			return;
19
		}
4166 leency 20
		mailsize = mailend - mailstart;
4186 leency 21
		debug("Getting QP");
4166 leency 22
		if (strstri(mailstart, "quoted-printable")!=0)
3368 leency 23
		{
4186 leency 24
			new_buf = malloc(mailsize);
25
			qp_decode stdcall (mailstart, new_buf, mailsize);
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
		}
4417 leency 35
		debug("ProcessBase64");
36
		ProcessBase64();
4186 leency 37
		debug("GetHeaders: From, To, Date, Subject");
3368 leency 38
		GetHeader(#from, "\nFrom:");
39
		GetHeader(#to,   "\nTo:");
40
		GetHeader(#date, "\nDate:");
41
		GetHeader(#subj, "\nSubject:");
4186 leency 42
		debug("Get mdata");
4417 leency 43
		mdata = strstr(mailstart, "\x0a\x0d") + 3;
4186 leency 44
		debug("ConvertToDOS");
4166 leency 45
		ConvertToDOS(mdata, mailstart);
4186 leency 46
		debug("FromHTMLtoTXT");
3368 leency 47
		FromHTMLtoTXT();
48
		letter_view.first = letter_view.count = 0;
49
 
50
		line_off = mdata;
51
		while (line_off>1)
52
		{
53
			line_off = GetNextLine(line_off);
54
			letter_view.count++;
55
		}
4186 leency 56
		debug("SetAtrFromCurr");
3413 leency 57
		atr.SetAtrFromCurr(mail_list.current+1);
3368 leency 58
		DrawMailBox();
59
	}
60
}
61
 
62
void ConvertToDOS(dword inbuf, searchin)
63
{
64
	dword dos_buf=0;
65
	if (use_iconv==1)
66
	{
67
		cur_charset = CH_CP866;
68
		if (strstri(searchin, "windows-1251")!=0) || (strstri(searchin, "windows1251")!=0)
69
			{ dos_buf = ChangeCharset("CP1251", "CP866", inbuf);    cur_charset = CH_CP1251;}
70
		else if (strstri(searchin, "koi8-")!=0)
71
			{ dos_buf = ChangeCharset("KOI8-RU", "CP866", inbuf);   cur_charset = CH_KOI8;}
72
		else if (strstri(searchin, "utf-8")!=0) || (strstri(searchin, "utf8")!=0)
73
			{ dos_buf = ChangeCharset("UTF-8", "CP866", inbuf);     cur_charset = CH_UTF8;}
74
		else if (strstri(searchin, "iso8859-5")!=0) || (strstri(searchin, "iso-8859-5")!=0)
75
			{ dos_buf = ChangeCharset("ISO8859-5", "CP866", inbuf); cur_charset = CH_ISO8859_5;}
76
		else if (strstri(searchin, "windows-1252")!=0) || (strstri(searchin, "windows1252")!=0)
77
			{ dos_buf = ChangeCharset("CP1252", "CP866", inbuf);    cur_charset = CH_CP1252;}
78
	}
79
	else
80
	{
81
		if (strstri(searchin, "windows-1251")!=0) wintodos( inbuf); else
82
		if (strstri(searchin, "koi8-")!=0)        koitodos( inbuf); else
83
		if (strstri(searchin, "utf-8")!=0)        utf8rutodos( inbuf);
84
	}
85
}
86
 
87
 
88
void FromHTMLtoTXT()
89
{
90
	dword cur_chr, txt_buf_srt, txt_buf_end, is_tag=0;
91
	int i;
4166 leency 92
	if (strstri(mdata, "")==0) && (strstri(mailstart, "text/html")==0) {debug("no html tags found"); return;}
4186 leency 93
	debug ("Mail got  code");
3368 leency 94
	cur_chr = mdata;
4166 leency 95
	txt_buf_srt = malloc(mailend - mailstart);
3368 leency 96
	txt_buf_end = txt_buf_srt;
97
 
4166 leency 98
	while (cur_chr < mailend)
3368 leency 99
	{
100
		if (DSBYTE[cur_chr]=='<') is_tag = 1;
101
		if (!is_tag)
102
		{
103
		 	DSBYTE[txt_buf_end] = DSBYTE[cur_chr];
104
		 	txt_buf_end++;
105
		 	_END:
106
		}
107
		if (DSBYTE[cur_chr]=='>') is_tag = NULL;
108
		cur_chr++;
109
	}
110
	DSBYTE[txt_buf_end] = '\0';
111
	strcpy(mdata, txt_buf_srt);
4166 leency 112
	mailend = strlen(mailstart) + mailstart;
3368 leency 113
	free(txt_buf_srt);
114
}
115
 
116
 
117
dword GetNextLine(dword start_offset)
118
{
119
	dword off_n = strstr(start_offset, "\n") + 1,  //разрыв строки
120
	      off_w = letter_view.w / 6 - 2 + start_offset, //max длинна скроки
121
	      off_m;
122
	off_m = off_w;
123
	if (off_n < off_w) return off_n;
124
	while (off_m > start_offset) //перенос по словам
125
	{
126
		if (DSBYTE[off_m]==' ') || (DSBYTE[off_m]=='\9') || (DSBYTE[off_m]=='-') return off_m;
127
		off_m--;
128
	}
129
	return off_w;
130
}
131
 
132
dword CopyBetweenOffsets(dword start, end) //do not forget to free(line) after use
133
{
134
	dword line, new_line;
135
	if (end <= start) return 0;
136
	line = new_line = malloc(end - start + 3);
137
	while (end > start)
138
	{
139
		DSBYTE[new_line] = DSBYTE[start];
140
		start++;
141
		new_line++;
142
	}
143
	DSBYTE[new_line] = '\0';
144
	return line;
145
}
146
 
4186 leency 147
 
3368 leency 148
void GetHeader(dword workstr, searchstr)
149
{
150
	char tmpbuf[512];
151
	dword Qoff;
4186 leency 152
	int q_start, b_start;
3368 leency 153
 
4166 leency 154
	strcpyb(mailstart, workstr, searchstr, "\n");
4417 leency 155
	/*
4186 leency 156
	debug(searchstr);
157
	debug(workstr);
158
	if (strlen(workstr)<8) return;
159
	q_start = strstri(workstr, "?Q?");
160
	b_start = strstri(workstr, "?B?");
161
	if (q_start)
3368 leency 162
	{
163
		qp_decode stdcall (workstr, #tmpbuf, strlen(workstr));
164
		ConvertToDOS(#tmpbuf, workstr);
165
		strcpy(workstr, #tmpbuf);
166
		Qoff = strstri(workstr, "?Q?");
167
		strcpy(workstr, Qoff);
168
	}
4186 leency 169
 
170
	//any text that goes after "?=" deletes now
171
	if (b_start)
3368 leency 172
	{
4186 leency 173
		base64_decode stdcall (b_start, #tmpbuf, strlen(b_start)-5);
3368 leency 174
		ConvertToDOS(#tmpbuf, workstr);
175
		strcpy(workstr, #tmpbuf);
176
	}
177
	if (strlen(workstr)+10*6-Form.cwidth>0) { workstr=Form.cwidth/6+workstr-12; DSBYTE[workstr]='\0';}
4417 leency 178
	*/
179
}
180
 
181
 
182
 
183
void ProcessBase64()
184
{
185
	int b_start, b_end, b_size, b_buf;
186
	int clean_mailstart;
187
 
188
	b_start = strstr(mailstart, "?B?");
189
	debug("b_size");
190
	debugi(b_start);
191
	if (b_start)
192
	{
193
		b_end = strstr(b_start, "?=");
194
		debug("b_end");
195
		debugi(b_end);
196
		b_size = b_end - b_start;
197
		debug("b_size");
198
		debugi(b_size);
199
		b_buf = malloc(b_size);
200
		strcpyb(mailstart, b_buf, "?B?", "?=");
201
		debug("b_buf");
202
		debug(b_buf);
203
 
204
		base64_decode stdcall (b_buf, b_buf, b_size-3);
205
		ConvertToDOS(b_buf, mailstart);
206
		if (b_size
207
 
208
		clean_mailstart = malloc(strlen(mailstart));
209
		strlcpy(clean_mailstart, mailstart, b_start-mailstart);
210
		strcat(clean_mailstart, b_buf);
211
		strcat(clean_mailstart, b_end+2);
212
		free(b_buf);
213
		free(mailstart);
214
		mailsize = strlen(clean_mailstart);
215
		mailstart = clean_mailstart;
216
		mailend = mailstart + mailsize;
217
		ProcessBase64();
218
	}
219
}
220
 
221
 
222
//