Subversion Repositories Kolibri OS

Rev

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