Subversion Repositories Kolibri OS

Rev

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

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