home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / unity / d23456 / SYNAPSE.ZIP / source / lib / MIMEinLn.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-07-19  |  5.6 KB  |  174 lines

  1. {==============================================================================|
  2. | Project : Delphree - Synapse                                   | 001.000.002 |
  3. |==============================================================================|
  4. | Content: Inline MIME support procedures and functions                        |
  5. |==============================================================================|
  6. | The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
  7. | (the "License"); you may not use this file except in compliance with the     |
  8. | License. You may obtain a Copy of the License at http://www.mozilla.org/MPL/ |
  9. |                                                                              |
  10. | Software distributed under the License is distributed on an "AS IS" basis,   |
  11. | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
  12. | the specific language governing rights and limitations under the License.    |
  13. |==============================================================================|
  14. | The Original Code is Synapse Delphi Library.                                 |
  15. |==============================================================================|
  16. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  17. | Portions created by Lukas Gebauer are Copyright (c)2000,2001.                |
  18. | All Rights Reserved.                                                         |
  19. |==============================================================================|
  20. | Contributor(s):                                                              |
  21. |==============================================================================|
  22. | History: see HISTORY.HTM from distribution package                           |
  23. |          (Found at URL: http://www.ararat.cz/synapse/)                       |
  24. |==============================================================================}
  25.  
  26. {$WEAKPACKAGEUNIT ON}
  27.  
  28. unit MIMEinLn;
  29.  
  30. interface
  31.  
  32. uses
  33.   SysUtils, Classes,
  34.   SynaChar, SynaCode, SynaUtil;
  35.  
  36. function InlineDecode(const Value: string; CP: TMimeChar): string;
  37. function InlineEncode(const Value: string; CP, MimeP: TMimeChar): string;
  38. function NeedInline(const Value: string): boolean;
  39. function InlineCode(const Value: string): string;
  40. function InlineEmail(const Value: string): string;
  41.  
  42. implementation
  43.  
  44. {==============================================================================}
  45.  
  46. function InlineDecode(const Value: string; CP: TMimeChar): string;
  47. var
  48.   s, su: string;
  49.   x, y, z, n: Integer;
  50.   ichar: TMimeChar;
  51.   c: Char;
  52.  
  53.   function SearchEndInline(const Value: string; be: Integer): Integer;
  54.   var
  55.     n, q: Integer;
  56.   begin
  57.     q := 0;
  58.     Result := 0;
  59.     for n := be + 2 to Length(Value) - 1 do
  60.       if Value[n] = '?' then
  61.       begin
  62.         Inc(q);
  63.         if (q > 2) and (Value[n + 1] = '=') then
  64.         begin
  65.           Result := n;
  66.           Break;
  67.         end;
  68.       end;
  69.   end;
  70.  
  71. begin
  72.   Result := Value;
  73.   x := Pos('=?', Result);
  74.   y := SearchEndInline(Result, x);
  75.   while y > x do
  76.   begin
  77.     s := Copy(Result, x, y - x + 2);
  78.     su := Copy(s, 3, Length(s) - 4);
  79.     ichar := GetCPFromID(su);
  80.     z := Pos('?', su);
  81.     if (Length(su) >= (z + 2)) and (su[z + 2] = '?') then
  82.     begin
  83.       c := UpperCase(su)[z + 1];
  84.       su := Copy(su, z + 3, Length(su) - z - 2);
  85.       if c = 'B' then
  86.       begin
  87.         s := DecodeBase64(su);
  88.         s := CharsetConversion(s, ichar, CP);
  89.       end;
  90.       if c = 'Q' then
  91.       begin
  92.         s := '';
  93.         for n := 1 to Length(su) do
  94.           if su[n] = '_' then
  95.             s := s + ' '
  96.           else
  97.             s := s + su[n];
  98.         s := DecodeQuotedPrintable(s);
  99.         s := CharsetConversion(s, ichar, CP);
  100.       end;
  101.     end;
  102.     Result := Copy(Result, 1, x - 1) + s +
  103.       Copy(Result, y + 2, Length(Result) - y - 1);
  104.     x := Pos('=?', Result);
  105.     y := SearchEndInline(Result, x);
  106.   end;
  107. end;
  108.  
  109. {==============================================================================}
  110.  
  111. function InlineEncode(const Value: string; CP, MimeP: TMimeChar): string;
  112. var
  113.   s, s1: string;
  114.   n: Integer;
  115. begin
  116.   s := CharsetConversion(Value, CP, MimeP);
  117.   s := EncodeQuotedPrintable(s);
  118.   s1 := '';
  119.   for n := 1 to Length(s) do
  120.     if s[n] = ' ' then
  121.       s1 := s1 + '=20'
  122.     else
  123.       s1 := s1 + s[n];
  124.   Result := '=?' + GetIdFromCP(MimeP) + '?Q?' + s1 + '?=';
  125. end;
  126.  
  127. {==============================================================================}
  128.  
  129. function NeedInline(const Value: string): boolean;
  130. var
  131.   n: Integer;
  132. begin
  133.   Result := False;
  134.   for n := 1 to Length(Value) do
  135.     if Value[n] in (SpecialChar + [Char(1)..Char(31), Char(128)..Char(255)]) then
  136.     begin
  137.       Result := True;
  138.       Break;
  139.     end;
  140. end;
  141.  
  142. {==============================================================================}
  143.  
  144. function InlineCode(const Value: string): string;
  145. var
  146.   c: TMimeChar;
  147. begin
  148.   if NeedInline(Value) then
  149.   begin
  150.     c := IdealCharsetCoding(Value, GetCurCP,
  151.       [ISO_8859_1, ISO_8859_2, ISO_8859_3, ISO_8859_4, ISO_8859_5,
  152.       ISO_8859_6, ISO_8859_7, ISO_8859_8, ISO_8859_9, ISO_8859_10]);
  153.     Result := InlineEncode(Value, GetCurCP, c);
  154.   end
  155.   else
  156.     Result := Value;
  157. end;
  158.  
  159. {==============================================================================}
  160.  
  161. function InlineEmail(const Value: string): string;
  162. var
  163.   sd, se: string;
  164. begin
  165.   sd := GetEmailDesc(Value);
  166.   se := GetEmailAddr(Value);
  167.   if sd = '' then
  168.     Result := se
  169.   else
  170.     Result := '"' + InlineCode(sd) + '"<' + se + '>';
  171. end;
  172.  
  173. end.
  174.