Subversion Repositories Kolibri OS

Rev

Rev 3413 | 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 ( mailpointer-mailbuffer>9 ) if (strncmp(mailpointer-5,"\n.\n",5)==0) // note that c-- assembles "\n.\n" to 0x0d, 0x0a, 0x2e, 0x0d, 0x0a
  9.         {
  10.                 debug("End of mail detected");
  11.                 mailpointer = mailpointer - 5;
  12.                 *mailpointer='\0';
  13.                
  14.                 if (strstr(mailbuffer, "+OK")!=mailbuffer)
  15.                 {
  16.                         aim = GET_ANSWER_RETR;
  17.                         mailpointer = mailbuffer;
  18.                         debug("GET_ANSWER_RETR != +OK, retry GET_ANSWER_RETR");
  19.                         return;
  20.                 }
  21.                 aim=NULL;
  22.                 DSBYTE[mailpointer+1] = '\0';
  23.                 debug("Real letter size:");
  24.                 debugi(mailpointer - mailbuffer);
  25.  
  26.                 if (strstri(mailbuffer, "quoted-printable")!=0)
  27.                 {
  28.                         debug ("getting qp");
  29.                         new_buf = malloc(mailpointer-mailbuffer);
  30.                         qp_decode stdcall (mailbuffer, new_buf, mailpointer-mailbuffer);
  31.                         if (EAX==-1) debug("Too small buffer to convert QUOTED-PRINTABLE");
  32.                         else
  33.                         {
  34.                                 mailbuffer = free(mailbuffer);
  35.                                 mailbuffer = new_buf;
  36.                                 mailpointer = strlen(mailbuffer) + mailbuffer;
  37.                         }
  38.                 }
  39.                 debug ("getting list info");
  40.                 GetHeader(#from, "\nFrom:");
  41.                 GetHeader(#to,   "\nTo:");
  42.                 GetHeader(#date, "\nDate:");
  43.                 GetHeader(#subj, "\nSubject:");
  44.                 mdata = strstr(mailbuffer, "\n\r") + 3;
  45.                 debug ("converting to dos");
  46.                 ConvertToDOS(mdata, mailbuffer);
  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.                 }
  56.                 atr.SetAtrFromCurr(mail_list.current+1);
  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;
  91.         if (strstri(mdata, "<html>")==0) && (strstri(mailbuffer, "text/html")==0) {debug("no html tags found"); return;}
  92.         debug ("converting: html -> txt");
  93.         cur_chr = mdata;
  94.         txt_buf_srt = malloc(mailpointer - mailbuffer);
  95.         txt_buf_end = txt_buf_srt;
  96.  
  97.         while (cur_chr < mailpointer)
  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);
  111.         mailpointer = strlen(mailbuffer) + mailbuffer; //тупо везде это ставить
  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.  
  146. void GetHeader(dword workstr, searchstr)
  147. {
  148.         char tmpbuf[512];
  149.         dword Qoff;
  150.  
  151.         strcpyb(mailbuffer, workstr, searchstr, "\n");
  152.         if (strstri(workstr, "?Q?"))
  153.         {
  154.                 qp_decode stdcall (workstr, #tmpbuf, strlen(workstr));
  155.                 ConvertToDOS(#tmpbuf, workstr);
  156.                 strcpy(workstr, #tmpbuf);
  157.                 Qoff = strstri(workstr, "?Q?");
  158.                 strcpy(workstr, Qoff);
  159.         }
  160.         if (strstr(workstr, "?B?"))
  161.         {
  162.                 base64_decode stdcall (strstri(workstr, "?B?"), #tmpbuf, strlen(workstr));
  163.                 ConvertToDOS(#tmpbuf, workstr);
  164.                 strcpy(workstr, #tmpbuf);
  165.         }
  166.         if (strlen(workstr)+10*6-Form.cwidth>0) { workstr=Form.cwidth/6+workstr-12; DSBYTE[workstr]='\0';}
  167. }