EncoderBox.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 23934: EncoderBox.pas
  11. {
  12. { Rev 1.1 04/10/2003 15:22:18 CCostelloe
  13. { Emails generated now have the same date
  14. }
  15. {
  16. { Rev 1.0 26/09/2003 00:04:08 CCostelloe
  17. { Initial
  18. }
  19. unit EncoderBox;
  20. interface
  21. {$I IdCompilerDefines.inc}
  22. uses
  23. IndyBox,
  24. Classes,
  25. IdComponent, IdGlobal, IdSocketHandle, IdIntercept, IdMessage, IdMessageClient,
  26. SysUtils;
  27. type
  28. TEncoderBox = class(TIndyBox)
  29. protected
  30. FExtractPath: string;
  31. FMsg: TIdMessage;
  32. FGeneratedStream: TMemoryStream;
  33. FTestMessageName: string;
  34. public
  35. procedure Test; override;
  36. procedure TestMessage(const APathname: string; const AVerify: Boolean = False;
  37. const AEmit: Boolean = False);
  38. //
  39. property ExtractPath: string read FExtractPath;
  40. property Msg: TIdMessage read FMsg;
  41. property GeneratedStream: TMemoryStream read FGeneratedStream;
  42. property TestMessageName: string read FTestMessageName;
  43. end;
  44. implementation
  45. uses
  46. IdMessageCoderMIME, IdMessageCoderUUE, IdPOP3,
  47. IniFiles, IdText, IdAttachmentFile{$IFDEF VER130},FileCtrl{$ENDIF};
  48. { TEncoderBox }
  49. procedure TEncoderBox.Test;
  50. var
  51. i: integer;
  52. LRec: TSearchRec;
  53. LPathToSearch: string;
  54. begin
  55. LPathToSearch := GetDataDir + '*.ini';
  56. i := FindFirst(LPathToSearch, faAnyFile, LRec); try
  57. while i = 0 do begin
  58. TestMessage(GetDataDir + LRec.Name, True);
  59. i := FindNext(LRec);
  60. end;
  61. finally FindClose(LRec); end;
  62. end;
  63. procedure TEncoderBox.TestMessage(const APathname: string; const AVerify: Boolean = False;
  64. const AEmit: Boolean = False);
  65. var
  66. IniParams: TStringList;
  67. CorrectStream: TFileStream;
  68. //GeneratedStreamToFile: TFileStream;
  69. i: Integer;
  70. sTemp: string;
  71. sParentPart, sContentType, sType, sEncoding, sFile: string;
  72. nParentPart: Integer;
  73. nPos: integer;
  74. TheTextPart: TIdText;
  75. {$IFDEF INDY100}
  76. TheAttachment: TIdAttachmentFile;
  77. {$ELSE}
  78. TheAttachment: TIdAttachment;
  79. {$ENDIF}
  80. sr: TSearchRec;
  81. FileAttrs: Integer;
  82. procedure CompareStream(const AStream1: TStream; const AStream2: TStream; const AMsg: string);
  83. //var
  84. //i: integer;
  85. //LByte1, LByte2: byte;
  86. begin
  87. Check(AStream1.Size = AStream2.Size, 'File size mismatch with ' + AMsg);
  88. //The following always fails for MIME because the random boundary is always different !!!
  89. {
  90. for i := 1 to AStream1.Size do begin
  91. AStream1.ReadBuffer(LByte1, 1);
  92. AStream2.ReadBuffer(LByte2, 1);
  93. Check(LByte1 = LByte2, 'Mismatch at byte ' + IntToStr(i) + ', '
  94. + AMsg);
  95. end;
  96. }
  97. end;
  98. begin
  99. FTestMessageName := '';
  100. Status('Testing message ' + ExtractFilename(APathname));
  101. //Set up path to test directory...
  102. FExtractPath := ChangeFileExt(APathname, '') + GPathSep;
  103. ForceDirectories(ExtractPath);
  104. //Set up the filename of the correct (test) message...
  105. FTestMessageName := ExtractPath+ChangeFileExt(ExtractFilename(APathname), '.msg');
  106. //If it is Emit, make sure we will be able to delete the message...
  107. FileAttrs := 0; //Stop compiler whining it might not have been initialized
  108. if AEmit then begin
  109. if FindFirst(FTestMessageName, FileAttrs, sr) = 0 then begin
  110. if (sr.Attr and faReadOnly) = faReadOnly then begin
  111. raise EBXCheck.Create('The reference file exists and is read-only, Emit not valid: '+FTestMessageName);
  112. end;
  113. FindClose(sr);
  114. end;
  115. end;
  116. FMsg := TIdMessage.Create(Self);
  117. //Read in the INI settings that define the email we are to generate...
  118. IniParams := TStringList.Create;
  119. IniParams.LoadFromFile(APathname);
  120. //Make sure the date will always be the same, else get different
  121. //outputs for the Date header...
  122. FMsg.UseNowForDate := False;
  123. FMsg.Date := EncodeDate(2011, 11, 11);
  124. i := 0;
  125. while IniParams.Values['Body'+IntToStr(i)] <> '' do begin
  126. FMsg.Body.Add(IniParams.Values['Body'+IntToStr(i)]);
  127. Inc(i);
  128. end;
  129. FMsg.ContentTransferEncoding := IniParams.Values['ContentTransferEncoding'];
  130. if IniParams.Values['ConvertPreamble'] = 'True' then begin
  131. FMsg.ConvertPreamble := True;
  132. end else if IniParams.Values['ConvertPreamble'] = 'False' then begin
  133. FMsg.ConvertPreamble := False;
  134. end;
  135. if IniParams.Values['Encoding'] = 'meMIME' then begin
  136. FMsg.Encoding := meMIME;
  137. end else if IniParams.Values['Encoding'] = 'meUU' then begin
  138. FMsg.Encoding := meUU;
  139. end else if IniParams.Values['Encoding'] = 'meXX' then begin
  140. FMsg.Encoding := meXX;
  141. end;
  142. if IniParams.Values['ContentType'] <> '' then begin
  143. FMsg.ContentType := IniParams.Values['ContentType'];
  144. end;
  145. i := 0;
  146. while IniParams.Values['Part'+IntToStr(i)] <> '' do begin
  147. sTemp := IniParams.Values['Part'+IntToStr(i)];
  148. nPos := Pos(',', sTemp);
  149. sType := Copy(sTemp, 1, nPos-1);
  150. sTemp := Copy(sTemp, nPos+1, MAXINT);
  151. nPos := Pos(',', sTemp);
  152. sEncoding := Copy(sTemp, 1, nPos-1);
  153. sFile := Copy(sTemp, nPos+1, MAXINT);
  154. nParentPart := -999;
  155. nPos := Pos(',', sFile);
  156. if nPos > 0 then begin //ParentPart, ContentType optional
  157. sTemp := Copy(sFile, nPos+1, MAXINT);
  158. sFile := Copy(sFile, 1, nPos-1);
  159. nPos := Pos(',', sTemp);
  160. sContentType := Copy(sTemp, nPos+1, MAXINT);
  161. sParentPart := Copy(sTemp, 1, nPos-1);
  162. nParentPart := StrToInt(sParentPart);
  163. end;
  164. if sType = 'TIdText' then begin
  165. TheTextPart := TIdText.Create(FMsg.MessageParts);
  166. TheTextPart.Body.LoadFromFile(sFile);
  167. if sEncoding <> 'Default' then TheTextPart.ContentTransfer := sEncoding;
  168. if ((sContentType <> '') and (sContentType <> 'Default')) then TheTextPart.ContentType := sContentType;
  169. {$IFDEF INDY100}
  170. if nParentPart <> -999 then TheTextPart.ParentPart := nParentPart;
  171. {$ENDIF}
  172. end else begin
  173. {$IFDEF INDY100}
  174. TheAttachment := TIdAttachmentFile.Create(FMsg.MessageParts, sFile);
  175. {$ELSE}
  176. TheAttachment := TIdAttachment.Create(FMsg.MessageParts, sFile);
  177. {$ENDIF}
  178. if sEncoding <> 'Default' then TheAttachment.ContentTransfer := sEncoding;
  179. if ((sContentType <> '') and (sContentType <> 'Default')) then TheAttachment.ContentType := sContentType;
  180. {$IFDEF INDY100}
  181. if nParentPart <> -999 then TheAttachment.ParentPart := nParentPart;
  182. {$ENDIF}
  183. end;
  184. Inc(i);
  185. end;
  186. //Do the test...
  187. FGeneratedStream := TMemoryStream.Create;
  188. FMsg.SaveToStream(FGeneratedStream);
  189. //Compare the results...
  190. try
  191. if AEmit then begin
  192. GeneratedStream.Seek(0, soFromBeginning);
  193. GeneratedStream.SaveToFile(TestMessageName);
  194. end else if AVerify then begin
  195. Check(FileExists(TestMessageName) = True, 'Missing correct result file '+TestMessageName);
  196. CorrectStream := TFileStream.Create(TestMessageName, fmOpenRead);
  197. GeneratedStream.Seek(0, soFromBeginning);
  198. CompareStream(GeneratedStream, CorrectStream, ExtractFilename(APathname));
  199. end;
  200. finally FreeAndNil(CorrectStream); end;
  201. Status('Message encoded.');
  202. end;
  203. initialization
  204. TIndyBox.RegisterBox(TEncoderBox, 'Emails', 'Encoders');
  205. end.