Subversion Repositories Kolibri OS

Rev

Rev 4142 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. //Leency & SoUrcerer, LGPL
  2.  
  3. void ParseMail()
  4. {
  5.         dword line_off, new_buf;
  6.         char tline[256];
  7.  
  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
  9.         {
  10.                 mailend -= 5;
  11.                 if (strstr(mailstart, "+OK")!=mailstart)
  12.                 {
  13.                         aim = GET_ANSWER_RETR;
  14.                         mailend = mailstart;
  15.                         debug("GET_ANSWER_RETR != +OK, retry GET_ANSWER_RETR");
  16.                         return;
  17.                 }
  18.                 aim=NULL;
  19.                 DSBYTE[mailend] = '\0';
  20.                 mailsize = mailend - mailstart;
  21.  
  22.                 if (strstri(mailstart, "quoted-printable")!=0)
  23.                 {
  24.                         new_buf = malloc(mailend-mailstart);
  25.                         qp_decode stdcall (mailstart, new_buf, mailend-mailstart);
  26.                         if (EAX==-1) debug("Too small buffer to convert QUOTED-PRINTABLE");
  27.                         else
  28.                         {
  29.                                 free(mailstart);
  30.                                 mailstart = new_buf;
  31.                                 mailsize = strlen(mailstart);
  32.                                 mailend = mailsize + mailstart;
  33.                         }
  34.                 }
  35.                 GetHeader(#from, "\nFrom:");
  36.                 GetHeader(#to,   "\nTo:");
  37.                 GetHeader(#date, "\nDate:");
  38.                 GetHeader(#subj, "\nSubject:");
  39.                 mdata = strstr(mailstart, "\n\r") + 3;          // 0x0d 0x0a, 0x0a
  40.                 ConvertToDOS(mdata, mailstart);
  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.                 }
  50.                 atr.SetAtrFromCurr(mail_list.current+1);
  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;
  85.         if (strstri(mdata, "<html>")==0) && (strstri(mailstart, "text/html")==0) {debug("no html tags found"); return;}
  86.         debug ("converting: html -> txt");
  87.         cur_chr = mdata;
  88.         txt_buf_srt = malloc(mailend - mailstart);
  89.         txt_buf_end = txt_buf_srt;
  90.  
  91.         while (cur_chr < mailend)
  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);
  105.         mailend = strlen(mailstart) + mailstart;
  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.  
  145.         strcpyb(mailstart, workstr, searchstr, "\n");
  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. }